541 lines
15 KiB
Java
541 lines
15 KiB
Java
package com.system.util;
|
||
|
||
import java.io.BufferedInputStream;
|
||
import java.io.BufferedOutputStream;
|
||
import java.io.BufferedReader;
|
||
import java.io.ByteArrayOutputStream;
|
||
import java.io.File;
|
||
import java.io.FileInputStream;
|
||
import java.io.FileOutputStream;
|
||
import java.io.FileWriter;
|
||
import java.io.IOException;
|
||
import java.io.InputStream;
|
||
import java.io.InputStreamReader;
|
||
import java.io.OutputStream;
|
||
import java.io.OutputStreamWriter;
|
||
import java.io.PrintWriter;
|
||
import java.io.RandomAccessFile;
|
||
import java.nio.MappedByteBuffer;
|
||
import java.nio.channels.FileChannel;
|
||
import java.nio.channels.FileChannel.MapMode;
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
|
||
import javax.servlet.http.HttpServletRequest;
|
||
import javax.servlet.http.HttpServletResponse;
|
||
|
||
import org.springframework.web.multipart.MultipartFile;
|
||
|
||
|
||
|
||
public class FileUtil {
|
||
|
||
/**
|
||
* 单个文件上传
|
||
* @param is
|
||
* @param fileName
|
||
* @param filePath
|
||
*/
|
||
public static boolean upFile(MultipartFile uploadFile,String fileName,String filePath){
|
||
FileOutputStream fos = null;
|
||
BufferedOutputStream bos = null;
|
||
FileInputStream is = null;
|
||
BufferedInputStream bis = null;
|
||
File file = new File(filePath);
|
||
if(!file.exists()){
|
||
file.mkdirs();
|
||
}
|
||
File f = new File(filePath+"/"+fileName);
|
||
try {
|
||
InputStream inputStream = uploadFile.getInputStream();
|
||
//is = new FileInputStream(uploadFile);
|
||
bis = new BufferedInputStream(inputStream);
|
||
fos = new FileOutputStream(f);
|
||
bos = new BufferedOutputStream(fos);
|
||
byte[] bt = new byte[4096];
|
||
int len = 0;
|
||
while((len = bis.read(bt))>0){
|
||
bos.write(bt, 0, len);
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
return false;
|
||
}finally {
|
||
|
||
try {
|
||
if(null != bos){
|
||
bos.close();
|
||
bos = null;
|
||
}
|
||
if(null != fos){
|
||
fos.close();
|
||
fos= null;
|
||
}
|
||
if(null != is){
|
||
is.close();
|
||
is=null;
|
||
}
|
||
|
||
if (null != bis) {
|
||
bis.close();
|
||
bis = null;
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 文件下载
|
||
* @param response
|
||
* @param downloadFile
|
||
*/
|
||
public static void downloadFile(HttpServletRequest request, HttpServletResponse response, String downloadFile, String fileName) {
|
||
|
||
BufferedInputStream bis = null;
|
||
InputStream is = null;
|
||
OutputStream os = null;
|
||
BufferedOutputStream bos = null;
|
||
try {
|
||
File file=new File(downloadFile); //文件的声明
|
||
is = new FileInputStream(file); //文件流的声明
|
||
os = response.getOutputStream(); //重点突出(特别注意),通过response获取的输出流,作为服务端往客户端浏览器输出内容的一个通道
|
||
// 为了提高效率使用缓冲区流
|
||
bis = new BufferedInputStream(is);
|
||
bos = new BufferedOutputStream(os);
|
||
// 处理下载文件名的乱码问题
|
||
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
|
||
fileName = new String(fileName.getBytes("GB2312"),"ISO-8859-1");
|
||
} else {
|
||
// 对文件名进行编码处理中文问题
|
||
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 处理中文文件名的问题
|
||
fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
|
||
}
|
||
response.reset(); // 重点突出
|
||
response.setCharacterEncoding("UTF-8"); // 重点突出
|
||
response.setContentType("application/x-msdownload");// 不同类型的文件对应不同的MIME类型 // 重点突出
|
||
// inline在浏览器中直接显示,不提示用户下载
|
||
// attachment弹出对话框,提示用户进行下载保存本地
|
||
// 默认为inline方式
|
||
response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
|
||
// response.setHeader("Content-Disposition", "attachment; filename="+fileName); // 重点突出
|
||
int bytesRead = 0;
|
||
byte[] buffer = new byte[1024];
|
||
while ((bytesRead = bis.read(buffer)) != -1){ //重点
|
||
bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
|
||
}
|
||
|
||
} catch (Exception ex) {
|
||
throw new RuntimeException(ex.getMessage());
|
||
} finally {
|
||
// 特别重要
|
||
// 1. 进行关闭是为了释放资源
|
||
// 2. 进行关闭会自动执行flush方法清空缓冲区内容
|
||
try {
|
||
if (null != bis) {
|
||
bis.close();
|
||
bis = null;
|
||
}
|
||
if (null != bos) {
|
||
bos.close();
|
||
bos = null;
|
||
}
|
||
if (null != is) {
|
||
is.close();
|
||
is = null;
|
||
}
|
||
if (null != os) {
|
||
os.close();
|
||
os = null;
|
||
}
|
||
} catch (Exception ex) {
|
||
throw new RuntimeException(ex.getMessage());
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 文件下载
|
||
* @param response
|
||
* @param downloadFile
|
||
*/
|
||
public static void downloadFile(HttpServletResponse response, String downloadFile, String showFileName) {
|
||
|
||
BufferedInputStream bis = null;
|
||
InputStream is = null;
|
||
OutputStream os = null;
|
||
BufferedOutputStream bos = null;
|
||
try {
|
||
File file=new File(downloadFile); //:文件的声明
|
||
String fileName=file.getName();
|
||
is = new FileInputStream(file); //:文件流的声明
|
||
os = response.getOutputStream(); // 重点突出
|
||
bis = new BufferedInputStream(is);
|
||
bos = new BufferedOutputStream(os);
|
||
// 对文件名进行编码处理中文问题
|
||
fileName = java.net.URLEncoder.encode(showFileName, "UTF-8");// 处理中文文件名的问题
|
||
fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
|
||
response.reset(); // 重点突出
|
||
response.setCharacterEncoding("UTF-8"); // 重点突出
|
||
response.setContentType("application/x-msdownload");// 不同类型的文件对应不同的MIME类型 // 重点突出
|
||
// inline在浏览器中直接显示,不提示用户下载
|
||
// attachment弹出对话框,提示用户进行下载保存本地
|
||
// 默认为inline方式
|
||
response.setHeader("Content-Disposition", "attachment; filename="+fileName); // 重点突出
|
||
int bytesRead = 0;
|
||
byte[] buffer = new byte[1024];
|
||
while ((bytesRead = bis.read(buffer)) != -1){ //重点
|
||
bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
|
||
}
|
||
|
||
} catch (Exception ex) {
|
||
throw new RuntimeException(ex.getMessage());
|
||
} finally {
|
||
// 特别重要
|
||
// 1. 进行关闭是为了释放资源
|
||
// 2. 进行关闭会自动执行flush方法清空缓冲区内容
|
||
try {
|
||
if (null != bis) {
|
||
bis.close();
|
||
bis = null;
|
||
}
|
||
if (null != bos) {
|
||
bos.close();
|
||
bos = null;
|
||
}
|
||
if (null != is) {
|
||
is.close();
|
||
is = null;
|
||
}
|
||
if (null != os) {
|
||
os.close();
|
||
os = null;
|
||
}
|
||
} catch (Exception ex) {
|
||
throw new RuntimeException(ex.getMessage());
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 文件下载
|
||
* @param response
|
||
* @param downloadFile
|
||
*/
|
||
public static void downloadPDFFile(HttpServletResponse response, String showFileName, byte[] data) {
|
||
|
||
OutputStream os = null;
|
||
BufferedOutputStream bos = null;
|
||
try {
|
||
// 对文件名进行编码处理中文问题
|
||
/*showFileName = URLEncoder.encode(showFileName, "UTF-8");
|
||
if (showFileName.length() > 150) {
|
||
String guessCharset = "gb2312"; 根据request的locale 得出可能的编码,中文操作系统通常是gb2312
|
||
showFileName = new String(showFileName.getBytes("gb2312"), "ISO8859-1");
|
||
} */
|
||
|
||
showFileName = new String(showFileName.getBytes("utf-8"),"iso8859-1");
|
||
|
||
os = response.getOutputStream(); // 重点突出
|
||
bos = new BufferedOutputStream(os);
|
||
// 对文件名进行编码处理中文问题
|
||
response.reset(); // 重点突出
|
||
response.setHeader("Accept-Ranges", "bytes");
|
||
response.setHeader("Cache-Control", "no-store");
|
||
response.setHeader("Content-Length", data.length + "");
|
||
response.setHeader("Content-Type", "application/pdf");
|
||
//response.setHeader("X-Application-Context","application:8080");
|
||
response.setCharacterEncoding("UTF-8"); // 重点突出
|
||
//response.setContentType("application/pdf");// 不同类型的文件对应不同的MIME类型 // 重点突出
|
||
// inline在浏览器中直接显示,不提示用户下载
|
||
// attachment弹出对话框,提示用户进行下载保存本地
|
||
// 默认为inline方式
|
||
//attachment;filename= attachment;浏览器会自动下载
|
||
//response.setHeader("Content-Disposition", "filename=9999"); // 重点突出
|
||
bos.write(data);
|
||
bos.flush();
|
||
} catch (Exception ex) {
|
||
throw new RuntimeException(ex.getMessage());
|
||
} finally {
|
||
try {
|
||
if (null != bos) {
|
||
bos.close();
|
||
bos = null;
|
||
}
|
||
if (null != os) {
|
||
os.close();
|
||
os = null;
|
||
}
|
||
} catch (Exception ex) {
|
||
throw new RuntimeException(ex.getMessage());
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/**
|
||
* 将文本文件转换成2进制
|
||
* @param is
|
||
* @param fileName
|
||
* @param filePath
|
||
*/
|
||
public static byte[] convertFile2Byte(MultipartFile uploadFile){
|
||
try {
|
||
if(uploadFile == null){
|
||
return null;
|
||
}
|
||
return uploadFile.getBytes();
|
||
} catch (IOException e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
*
|
||
* @Title: convertFile2Base64Str
|
||
* @Description: 将上传文件解析成base64字符串
|
||
* @param uploadFile
|
||
* @return
|
||
* @return String 返回类型
|
||
* @throws
|
||
*/
|
||
public static String convertFile2Base64Str(MultipartFile uploadFile){
|
||
try {
|
||
return Base64Util.encode(convertFile2Byte(uploadFile));
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
return null;
|
||
}
|
||
|
||
|
||
public static String txt2String(File file){
|
||
StringBuilder result = new StringBuilder();
|
||
try{
|
||
InputStreamReader ir =new InputStreamReader(new FileInputStream(file), "utf-8");
|
||
BufferedReader br = new BufferedReader(ir);//构造一个BufferedReader类来读取文件
|
||
String s = null;
|
||
while((s = br.readLine())!=null){//使用readLine方法,一次读一行
|
||
result.append(new String(s.getBytes()));
|
||
}
|
||
br.close();
|
||
}catch(Exception e){
|
||
e.printStackTrace();
|
||
}
|
||
|
||
return result.toString();
|
||
}
|
||
|
||
public static String ReadFile(String Path){
|
||
BufferedReader reader = null;
|
||
String laststr = "";
|
||
try{
|
||
FileInputStream fileInputStream = new FileInputStream(Path);
|
||
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
|
||
reader = new BufferedReader(inputStreamReader);
|
||
String tempString = null;
|
||
while((tempString = reader.readLine()) != null){
|
||
laststr += tempString;
|
||
}
|
||
reader.close();
|
||
}catch(IOException e){
|
||
e.printStackTrace();
|
||
}finally{
|
||
if(reader != null){
|
||
try {
|
||
reader.close();
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
return laststr;
|
||
}
|
||
|
||
|
||
public static List<String> ReadLine(String Path){
|
||
BufferedReader reader = null;
|
||
List<String> lines = new ArrayList<String>();
|
||
try{
|
||
FileInputStream fileInputStream = new FileInputStream(Path);
|
||
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
|
||
reader = new BufferedReader(inputStreamReader);
|
||
|
||
String line = null;
|
||
while((line = reader.readLine()) != null){
|
||
lines.add(line);
|
||
}
|
||
reader.close();
|
||
}catch(IOException e){
|
||
e.printStackTrace();
|
||
}finally{
|
||
if(reader != null){
|
||
try {
|
||
reader.close();
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
return lines;
|
||
}
|
||
|
||
/**
|
||
*
|
||
* @Title: toByteArray3
|
||
* @Description: TODO
|
||
* @param filename
|
||
* @return
|
||
* @throws IOException
|
||
* @return byte[] 返回类型
|
||
* @throws
|
||
*/
|
||
@SuppressWarnings("resource")
|
||
public static byte[] file2ByteArray(String filename) throws IOException {
|
||
|
||
FileChannel fc = null;
|
||
try {
|
||
fc = new RandomAccessFile(filename, "r").getChannel();
|
||
MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0, fc.size()).load();
|
||
byte[] result = new byte[(int) fc.size()];
|
||
if (byteBuffer.remaining() > 0) {
|
||
byteBuffer.get(result, 0, byteBuffer.remaining());
|
||
}
|
||
return result;
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
throw e;
|
||
} finally {
|
||
try {
|
||
fc.close();
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
public static byte[] getFileToByte(File file) {
|
||
InputStream is = null;
|
||
ByteArrayOutputStream bytestream = null;
|
||
byte[] by = new byte[(int) file.length()];
|
||
try {
|
||
is = new FileInputStream(file);
|
||
bytestream = new ByteArrayOutputStream();
|
||
byte[] bb = new byte[2048];
|
||
int ch;
|
||
ch = is.read(bb);
|
||
while (ch != -1) {
|
||
bytestream.write(bb, 0, ch);
|
||
ch = is.read(bb);
|
||
}
|
||
by = bytestream.toByteArray();
|
||
} catch (Exception ex) {
|
||
ex.printStackTrace();
|
||
}finally {
|
||
try {
|
||
|
||
if(is != null) is.close();
|
||
if(bytestream != null) bytestream.close();
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
return by;
|
||
}
|
||
|
||
public static byte[] getFileToByte(String filename) {
|
||
File file = new File(filename);
|
||
if(!file.exists()) throw new RuntimeException("文件不存在");
|
||
return getFileToByte( file);
|
||
}
|
||
|
||
|
||
/**
|
||
*
|
||
* writeData: 将数据写入指定的文件中
|
||
*
|
||
* @param filePath
|
||
* @param data
|
||
* @return boolean 是否写入成功
|
||
*/
|
||
public static boolean writeData(String filePath, byte[] data){
|
||
FileOutputStream fos = null;
|
||
try {
|
||
File file = new File(filePath);
|
||
if(file.exists()){
|
||
file.delete();
|
||
}
|
||
file.createNewFile();
|
||
fos =new FileOutputStream(file);
|
||
fos.write(data);
|
||
fos.close();
|
||
return true;
|
||
} catch (Exception e) {
|
||
return false;
|
||
}finally{
|
||
if(fos != null){
|
||
try {
|
||
fos.close();
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* @param filePath
|
||
* @param data
|
||
* @param nextLine
|
||
* @return boolean 是否写入成功
|
||
*/
|
||
public static boolean append(String filePath, String data, boolean nextLine ){
|
||
try {
|
||
try {
|
||
File file = new File(filePath);
|
||
if(!file.exists()){
|
||
file.createNewFile();
|
||
}
|
||
FileOutputStream fos = new FileOutputStream(file, true);
|
||
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
|
||
if(nextLine) {
|
||
data = data + System.lineSeparator();
|
||
}
|
||
osw.write(data);
|
||
osw.flush();
|
||
osw.close();
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
return true;
|
||
} catch (Exception e) {
|
||
return false;
|
||
}finally{
|
||
}
|
||
|
||
}
|
||
public static void main(String[] args) throws IOException {
|
||
FileUtil.append("D:\\aa.txt", "12323",true);
|
||
}
|
||
|
||
}
|
||
|
||
|