我是用的文件流下载的在这要等20-30秒左右才行在IE没这种问题只有谷歌火狐才有(好像是转码的问题),我应该怎么解决,各位大佬帮帮忙!最新代码!
代码如下:
public void downLoad(int pid, HttpServletResponse response) throws IOException {
String path = "F:\\upload\\images" + "\\" + productService.getPimage(pid);
String filename = productService.getPname(pid);
//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data");
//2.设置文件头:最后一个参数是设置下载文件名
response.setHeader("Content-Disposition", "attachment;fileName="+new String(filename.getBytes("utf-8"), "ISO8859-1"));
ServletOutputStream out;
//通过文件路径获得File对象
File file = new File(path);
Date date = new Date();
System.out.println("date1"+date);
try {
FileInputStream inputStream = new FileInputStream(file);
//3.通过response获取ServletOutputStream对象(out)
out = response.getOutputStream();
System.out.println("date2"+date);
int b = 0;
byte[] buffer = new byte[1024*10];
while (b != -1){
b = inputStream.read(buffer);
//4.写到输出流(out)中
out.write(buffer,0,b);
}
System.out.println("date3"+date);
inputStream.close();
out.close();
out.flush();
System.out.println("date4"+date);
} catch (IOException e) {
e.printStackTrace();
}
}
用buffered缓冲
public void download(String path, HttpServletRequest request, HttpServletResponse response) {
try {
String url="D:\\download\\3024IVRFailRecords20190502.txt";
File file = new File(url);
InputStream fis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
// 设置response的Header
response.addHeader("Content-Length", "" + file.length());
response.setHeader("Content-Disposition", "attachment; filename="+file.getName());
//如果是图片可以开启
// response.setContentType("image/jpg");
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
//return response;
}
用这个,上传的代码我也有 需要的话可以私信我,
望采纳