jspSmartUpload 下载。

mybydhn 2011-06-11 11:58:14
做了一个文件列表,让用户可以下载文件。用jspSmartload如何实现。
...全文
27 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
mybydhn 2011-06-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 huangrong520raoting 的回复:]
/**
* 下载文件
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
@SuppressWarnings("deprecation")
public ActionForward doDownload(ActionMapping mapping……
[/Quote]这一大串对我没用。还是把分给你吧。。
Double 2011-06-12
  • 打赏
  • 举报
回复
/**
* 下载文件
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
@SuppressWarnings("deprecation")
public ActionForward doDownload(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
FileForm fileForm = (FileForm) form;
OutputStream toClient = null;
try {
//测试--设置为固定路径
String path = request.getRealPath("/") + "upload\\" + fileForm.getFileName();
File file = new File(path);
String filename = file.getName();

// 取得文件的扩展名
String ext = filename.substring(filename.lastIndexOf(".") + 1)
.toUpperCase();

InputStream fis = null;
try{
fis = new BufferedInputStream(new FileInputStream(path));
}catch(FileNotFoundException e){
e.printStackTrace();
System.out.println("系统没有找到指定文件");
}
if(fis==null){
response.getWriter().print("<script>alert('系统没有找到指定文件!');history.go(-1);</script>");
return null;
}
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();

response.reset();
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length()); // 设置返回的文件类型
toClient = new BufferedOutputStream(response
.getOutputStream()); // 得到向客户端输出二进制数据的对象
// 根据扩展名声称客户端浏览器mime类型
if (ext.equals("DOC"))
response.setContentType("application/msword");
else
response.setContentType("application/octet-stream"); // 设置返回的文件类型
try{
if(toClient != null){
toClient.write(buffer); // 输出数据
toClient.flush();
}
}catch(Exception e){
e.printStackTrace();
System.out.println("用户取消下载文件...");
}
} catch (IOException ex) {
ex.printStackTrace();
} finally{
try {
if(toClient!=null){
toClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

你看看这个对你有用没

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧