try {
//創(chuàng)建一個帶有路徑的文件
File f = new File(fileDiskURL+fileName);//getDataExtractPath()
//創(chuàng)建一個與文件對應的輸入流
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;
}
回復 更多評論