//单个文件下载
/**
* 根据filePath和fileName下载稿件
*/
public StringfilePath;
public StringfileName;
public StringdownLoadDraft(HttpServletRequest request, HttpServletResponse response){
try {
Stringfile_prefix = constantBean.propertiesMap.get("file_path");
StringallPath = file_prefix+File.separator+ filePath; File ffile =new File(allPath);
if(!ffile.exists()){
message = "下载失败,该资源已被删除!";
return"send_draft_info.jsp";
}
//设置响应头,控制浏览器下载该文件
response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8"));
//读取要下载的文件,保存到文件输入流
FileInputStream in = newFileInputStream(allPath);
//创建输出流
OutputStream out =response.getOutputStream();
//创建缓冲区
byte buffer[] =new byte[1024];
int len = 0;
//循环将输入流中的内容读取到缓冲区当中
while((len=in.read(buffer))>0){
//输出缓冲区的内容到浏览器,实现文件下载
out.write(buffer, 0, len);
}
//关闭文件输入流
in.close();
//关闭输出流
out.close();
}catch(UnsupportedEncodingException e) {
e.printStackTrace();
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
}
message = "下载成功!";
return"send_receive_draft.jsp";
}