Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
2b628c26
Commit
2b628c26
authored
Apr 21, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
文件处理
parent
f4038634
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
187 additions
and
0 deletions
+187
-0
FileHelper.java
...src/main/java/com/yanzuoguang/util/helper/FileHelper.java
+108
-0
HttpFileHelper.java
...ain/java/com/yanzuoguang/cloud/helper/HttpFileHelper.java
+79
-0
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/FileHelper.java
0 → 100644
View file @
2b628c26
package
com
.
yanzuoguang
.
util
.
helper
;
import
com.yanzuoguang.util.exception.CodeException
;
import
java.io.BufferedReader
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.io.RandomAccessFile
;
/**
* 登录相关函数
*/
public
class
FileHelper
{
/**
* 获取登录标记
*
* @param file 文件路径
* @param encoding 文件编码
* @return 读取的内容
*/
public
static
String
readFile
(
File
file
,
String
encoding
)
{
//检查此路径名的文件是否是一个目录(文件夹)
if
(
file
.
isDirectory
())
{
return
StringHelper
.
EMPTY
;
}
if
(!
file
.
exists
())
{
return
StringHelper
.
EMPTY
;
}
try
{
InputStream
inputStream
=
new
FileInputStream
(
file
);
return
readFile
(
inputStream
,
encoding
);
}
catch
(
CodeException
e
)
{
throw
e
;
}
catch
(
Exception
e
)
{
throw
new
CodeException
(
e
);
}
}
/**
* 读取文件内容
*
* @param inputStream 文件流
* @param encoding 文件编码
* @return 文件内容的结果
*/
public
static
String
readFile
(
InputStream
inputStream
,
String
encoding
)
{
if
(
inputStream
==
null
)
{
return
StringHelper
.
EMPTY
;
}
StringBuilder
sb
=
new
StringBuilder
();
try
{
try
{
InputStreamReader
inputStreamReader
=
new
InputStreamReader
(
inputStream
,
encoding
);
BufferedReader
bufferedReader
=
new
BufferedReader
(
inputStreamReader
);
String
line
;
//分行读取
while
((
line
=
bufferedReader
.
readLine
())
!=
null
)
{
if
(
sb
.
length
()
>
0
)
{
sb
.
append
(
"\n"
);
}
sb
.
append
(
line
);
}
}
finally
{
inputStream
.
close
();
//关闭输入流
}
}
catch
(
CodeException
e
)
{
throw
e
;
}
catch
(
Exception
e
)
{
throw
new
CodeException
(
e
);
}
return
sb
.
toString
();
}
/**
* 写入文件内容
*
* @param file 文件路径
* @param content 文件内容
* @param encoding 文件编码
*/
public
static
void
writeFile
(
File
file
,
String
content
,
String
encoding
)
{
try
{
// 删除文件
if
(
file
.
exists
())
{
file
.
delete
();
}
if
(
file
.
exists
())
{
throw
new
CodeException
(
"文件删除失败"
);
}
// 创建文件
file
.
getParentFile
().
mkdirs
();
file
.
createNewFile
();
// 写入文件
RandomAccessFile
raf
=
new
RandomAccessFile
(
file
,
"rwd"
);
raf
.
seek
(
file
.
length
());
raf
.
write
(
content
.
getBytes
(
encoding
));
raf
.
close
();
}
catch
(
CodeException
e
)
{
throw
e
;
}
catch
(
Exception
e
)
{
throw
new
CodeException
(
e
);
}
}
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/helper/HttpFileHelper.java
0 → 100644
View file @
2b628c26
package
com
.
yanzuoguang
.
cloud
.
helper
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.*
;
import
java.net.URL
;
import
java.net.URLConnection
;
/**
* HTTP文件
*/
public
class
HttpFileHelper
{
/**
* 下载文件
*
* @param serverFileName 下载的服务器文件路径
* @param localFileName 保存的文件路径
* @throws IOException
*/
public
static
void
downToLocal
(
String
serverFileName
,
String
localFileName
)
throws
IOException
{
int
byteRead
;
URL
url
=
new
URL
(
serverFileName
);
URLConnection
conn
=
url
.
openConnection
();
InputStream
inStream
=
conn
.
getInputStream
();
try
{
FileOutputStream
outputStream
=
new
FileOutputStream
(
localFileName
);
try
{
byte
[]
buffer
=
new
byte
[
1204
];
while
((
byteRead
=
inStream
.
read
(
buffer
))
!=
-
1
)
{
outputStream
.
write
(
buffer
,
0
,
byteRead
);
}
}
finally
{
outputStream
.
close
();
}
}
finally
{
inStream
.
close
();
}
}
/**
* 下载文件
*
* @param serverFilePath 下载文件来源路径
* @param saveFileName 文件保存名称
* @param response 输出对象
* @throws IOException 异常信息
*/
public
static
void
localToDown
(
String
serverFilePath
,
String
saveFileName
,
HttpServletResponse
response
)
throws
IOException
{
//获取文件的长度
File
file
=
new
File
(
serverFilePath
);
//设置文件输出类型
response
.
setContentType
(
"application/octet-stream"
);
// 设置下载的文件名
response
.
setHeader
(
"Content-disposition"
,
"attachment; filename="
+
new
String
(
saveFileName
.
getBytes
(
"utf-8"
),
"UTF-8"
));
//设置输出长度
response
.
setHeader
(
"Content-Length"
,
String
.
valueOf
(
file
.
length
()));
// 获取输入流
BufferedInputStream
bis
=
new
BufferedInputStream
(
new
FileInputStream
(
serverFilePath
));
try
{
BufferedOutputStream
bos
=
new
BufferedOutputStream
(
response
.
getOutputStream
());
//输出流
try
{
byte
[]
buff
=
new
byte
[
2048
];
int
readSize
=
1
;
while
(
readSize
>
0
)
{
readSize
=
bis
.
read
(
buff
,
0
,
buff
.
length
);
if
(
readSize
>
0
)
{
bos
.
write
(
buff
,
0
,
readSize
);
}
}
}
finally
{
bos
.
close
();
}
}
finally
{
bis
.
close
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment