/**
* @Title BaseAction.java
* @Package com.xdja
* @Description
* @author 张佳
* @date 2012-4-19 下午04:11:30
* @version v1.0
* @copyright Copyright 2012
* Company : 郑州信大捷安信息技术股份有限公司
*/
package com.xdja.wap;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URI;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport {
private static final long serialVersionUID = -72078345049879488L;
protected final Logger log = Logger.getLogger(BaseAction.class);
private InputStream inputStream;
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public InputStream getInputStream()throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Content-Disposition", "attachment;fileName="+ java.net.URLEncoder.encode(fileName,"UTF-8"));//header这样设置就能解决附件中文乱码问题
return inputStream;
}
private String fileName;// 要下载的附件名称
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
/**
* 下载附件
*
* @Title getDownLoad
* @Description
* @return
* @author 张佳
* @date 2012-4-24 上午09:43:11
*/
public void fileDownLoad(String fileName, String path)throws Exception {
FileInputStream inputSteam = null;
this.fileName = fileName;
File file = new File(path);
inputSteam = new FileInputStream(file);
this.setInputStream(inputSteam);
}
}
2.struts.xml配置
<!-- 新闻下载 -->
<action name="newsdownload" class="com.xdja.wap.news.NewsAction" method="newsDownLoad">
<result name="success" type="stream">
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="inputName">inputStream</param>
<param name="bufferSize">1024</param>
</result>
</action>
3.测试类
public String noticeDownLoad(){
try {
HttpServletRequest request = ServletActionContext.getRequest();
this.setFileName(new String(this.getFileName().getBytes("ISO-8859-1"), "UTF-8"));
//this.getFileName()接收前台传过来的文件名,这里为“张佳1234.doc”
String attachmentPath = request.getParameter("attPath");//获取附件存储的路径
if (!HelpFunctions.isEmpty(this.getFileName()) && !HelpFunctions.isEmpty(attachmentPath)) {
String path = SysConfigInfo.getElementAttributeValue("System/SnapShotPath", "value") + Constants.NOTICES_BASE_PATH + "/" + attachmentPath + "/" + this.getFileName();
//这里的path路径为G:\newswork\张佳1234.doc 也就是要下载的文件路径
this.fileDownLoad(this.getFileName(), path);//调用下载方法
}
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
return ERROR;
}
return SUCCESS;
}
4.效果图
晕 怎么上传图片?