`

struts上传

    博客分类:
  • J2EE
 
阅读更多
jsp页面
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<a href="xxxAction.do?fileId=...">点击下载</a>

struts.xml
<action name="xxxAction" class="downAction">
    <result name="success" type="stream" >
        <param name="fileName" >file</param>
         <param name="contentDisposition" >attachment;filename=${filename}</param>
         <param name="buffersize">1024</param>
    </result>
downAction
    private String filename;
    private InputStream file;
    private String fileId;
    public String execute(){
   ServletContext sc=ServletActionContext.getServletContext();
   String path=sc.getRealPath("upload");
   filename=""+fileId;
   file=new FileInputStream(path+File.separator+filename);
  return "success";

}


文件下载,在jsp页面加一段java代码,给你贴个例子:
jsp页面加上这段代码的话、就会弹出框框 提示你打开还是保存?
java.text.SimpleDateFormat tempDate = new java.text.SimpleDateFormat(
"yyyyMMddHHmmss");
String time = tempDate.format(new Date());
String fileName = "用户基本信息_" + time + ".doc";
fileName = new String(fileName.getBytes("GBK"), "ISO8859_1");
response.setHeader("Content-Disposition", "attachment;filename="
+ fileName);

response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics