FileUtils
package com.cmbchina.ccd.itpm.project.util;
import org.slf4j.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import static org.slf4j.LoggerFactory.getLogger;
public class FileUtils {
private static final Logger logger = getLogger(FileUtils.class);
public static void download(HttpServletResponse response, String filePath, String encode) {
response.setContentType("text/html;charset=" + encode);
String downLoadPath = filePath;
File file = new File(downLoadPath);
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(downLoadPath)); BufferedOutputStream bos = new BufferedOutputStream(
response.getOutputStream())) {
long fileLength = file.length();
String fileName = file.getName();
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes(encode), StandardCharsets.UTF_8));
response.setHeader("Content-Length", String.valueOf(fileLength));
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
logger.error(e.getMessage());
}
}
public HttpServletResponse download(String path, HttpServletResponse response) {
try {
File file = new File(path);
String filename = file.getName();
String ext = filename.substring(".".lastIndexOf(filename) + 1).toUpperCase();
byte[] buffer;
try (InputStream fis = new BufferedInputStream(new FileInputStream(path))) {
buffer = new byte[fis.available()];
fis.read(buffer);
}
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
logger.error("{}", ex);
}
return response;
}
public void downloadLocal(HttpServletResponse response) throws IOException {
String fileName = "Operator.doc";
try (InputStream inStream = new FileInputStream("c:/Operator.doc")) {
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0) {
response.getOutputStream().write(b, 0, len);
}
} catch (IOException e) {
logger.error("{}", e);
}
}
}
public void downloadNet() throws IOException {
int bytesum = 0;
int byteread;
URL url = new URL("windine.blogdriver.com/logo.gif");
URLConnection conn = url.openConnection();
try (InputStream inStream = conn.getInputStream(); FileOutputStream fs = new FileOutputStream("c:/abc.gif")) {
byte[] buffer = new byte[1204];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
fs.write(buffer, 0, byteread);
}
}
}
public void downLoadIsOnLine(String fileName, HttpServletRequest request, HttpServletResponse response, boolean isOnLine) throws IOException {
String absolutePath = fileName;
File f = new File(absolutePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
String userAgent = request.getHeader("user-agent").toLowerCase();
fileName = f.getName();
String downloadFileName;
if (userAgent.contains("msie") || userAgent.contains("like gecko")) {
downloadFileName = URLEncoder.encode((fileName), "UTF-8");
} else {
downloadFileName = new String((fileName).getBytes(StandardCharsets.UTF_8), "iso-8859-1");
}
byte[] buf = new byte[1024];
int len = 0;
response.reset();
if (isOnLine) {
URL u = new URL("file:///" + absolutePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + downloadFileName);
} else {
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + downloadFileName);
}
try (BufferedInputStream br = new BufferedInputStream(new FileInputStream(f)); OutputStream out = response.getOutputStream()) {
while ((len = br.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
}
}
简单使用
public void download(@RequestParam("fileName") String fileName, HttpServletResponse response) throws IOException {
ObjectStat stat = minIoUtil.statObject("", fileName);
InputStream in = minIoUtil.getObject("", fileName);
int index;
byte[] bytes = new byte[1024];
File filePath = new File(localhostPath);
if (!filePath.exists()) {
filePath.mkdirs();
}
FileOutputStream downloadFile = new FileOutputStream(localhostPath+ "\\" + fileName);
while ((index = in.read(bytes)) != -1) {
downloadFile.write(bytes, 0, index);
downloadFile.flush();
}
in.close();
downloadFile.close();
File file = new File(localhostPath+ "\\" + fileName);
if (file.exists() && file.isFile()) {
responseFile(response,localhostPath+ "\\" + fileName);
file.delete();
}
}
public void responseFile(HttpServletResponse response, String path) {
try {
byte[] b = FileUtils.readFileToByteArray(new File(path));
if (b == null) {
return;
}
System.out.println("获取流" + b);
OutputStream out = response.getOutputStream();
out.write(b);
out.flush();
out.close();
} catch (Exception var5) {
System.out.println("当前路径文件读取失败:" + path + var5);
var5.printStackTrace();
}
}