private void pushFile(File file, HttpServletResponse resp) throws IOException {
String fileName = file.getName();
InputStream is = null;
ServletOutputStream os = null;
try {
fileName = URLEncoder.encode(fileName,"UTF-8");
resp.reset();//清空輸出流
resp.setContentType("multipart/form-data");//定義輸出類型
resp.setHeader("Content-disposition", "attachment; filename=\""+fileName+"\"");//設定輸出文件頭
is = new FileInputStream(file);
os = resp.getOutputStream();
byte[] b = new byte[2048];
int length;
while((length = is.read(b))>0) {
os.write(b, 0, length);
}
os.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
is.close();
os.close();
}
}