try {
//创建一个带有路径的文件
File f = new File(fileDiskURL+fileName);//getDataExtractPath()
//创建一个与文件对应的输入流
FileInputStream in = new FileInputStream(f);
//得到一个响应,并对响应进行各种设置
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("ISO-8859-1"),"GBK"));
//fetch the file
// OutputStream op = response.getOutputStream();
// byte[] buf = new byte[in.available()];
// in.read(buf);
// op.write(buf);
// in.close();
// op.flush();
// op.close();
System.out.println("aaaaaaaaaaa11111111111aaaaaaaaaaaaaa");
int length = (int) f.length();
if (length != 0) {
byte[] buf = new byte[4096];
OutputStream op = response.getOutputStream();
//把ServletOutputStream 换成 java.io里的outputstream就可以了
//ServletOutputStream op = response.getOutputStream();
while ((in != null) && ((length = in.read(buf)) != -1)) {
op.write(buf, 0, length);
}
in.close();
op.flush();
op.close();
}
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
回复 更多评论