---
title: "struts上传"
author: "chokee"
date: "2014-08-22T15:37:51Z"
tags: []
categories: ["编程语言"]
slug: "struts"
draft: false
lastmod: "2014-08-22T15:37:51Z"
id: 2107429
url: "https://chokee.iteye.com/blog/2107429"
---

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");