package com.bgsnewlook.action;html
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;java
import java.io.InputStream;apache
import java.net.URLEncoder;jsp
public class Download extends ActionSupport {
private String inputPath;
private String contentType;
private String downFileName;this
public String getInputPath() {
return inputPath;
}.net
public void setInputPath(String inputPath) throws Exception{code
this.inputPath = new String(inputPath.getBytes("iso-8859-1"), "utf-8");xml
}htm
public String getContentType() {
return contentType;
}utf-8
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getDownFileName() {
return downFileName;
}
public InputStream getTargetFile()
{
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}
public void setDownFileName(String downFileName) throws Exception {
// this.downFileName = downFileName;
this.downFileName = new String(downFileName.getBytes("iso-8859-1"),"utf-8");
this.downFileName= URLEncoder.encode(this.downFileName,"utf-8");
}
}
struts配置文件struts.xml
<action name="download" class="com.bgsnewlook.action.Download" >
<result type="stream" name="success">
<param name="contentType">${contentType}</param>
<param name="inputName">targetFile</param>
<param name="contentDisposition">
filename=${downFileName}
</param>
</result>
---down.jsp
<%@ page contentType="text/html;charset=utf-8" language="java" pageEncoding="utf-8" %>
<html>
<head>
<title></title>
</head>
<body>
<a href="download?inputPath=/uploadfiles/1.jpg&contentType=image/jpeg&downFileName=中文">下載文件</a>
</body></html>