æè¿é¡¹ç®ä¸éè¦åä¸ä¸ªå¼åå¹³å°ï¼ç¬¬ä¸ä¸ªåè½å°±æ¯å®ç°æ档对å¤æä¾ä¸è½½åè½ï¼é¡¹ç®ä¸ç¨å°çæ¯Struts2æ¡æ¶ï¼å æ¤åäºä¸ä¸ªç®åçACTIONå¨æ¤è®°å½å¦ä¹ ã
第ä¸æ¥ï¼é¦å éè¦æ°å»ºä¸ä¸ªACTIONï¼
import ins.framework.web.Struts2Action;
import org.apache.commons.codec.binary.Base64;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* åä½æ¹ä¸è½½èµæå¹³å°æ©å±
* @author Tanyunlong on 2016/10/11.
*/
public class OpenPlatformAction extends Struts2Action {
//ä¸è½½é¡µé¢ç¹å»è·³è½¬åæ°
private String index;
//ä¸è½½æ件å
private String fileName;
//è¾å
¥æµ
private InputStream fileStream;
public String openPlatformMethod(){
System.out.println("å¼æ¾å¹³å°openPlatformMethod æ§è¡====");
if ("0".equals(index)){
return "success0";//跳转å°å¹³å°é¦é¡µ
}else if ("1".equals(index)){
return "success1";//è·³å°å¹³å°æ¦è¿°
}else if ("2".equals(index)){
return "success2";//è·³å°ä¸å¡æµç¨
}else if ("3".equals(index)){
return "success3";//è·³å°ä¸è½½æ¥å£ææ¡£
}
return null;
}
public InputStream download(){
if ("a".endsWith(fileName)){
fileName="ï¼éç¨çï¼.docx";
}else if ("b".endsWith(fileName)){
fileName="解ç.doc";
}else if ("c".endsWith(fileName)){
fileName="webserviceè°ç¨æ ·ä¾-JAVA.java";
}else if ("d".endsWith(fileName)){
fileName="webserviceè°ç¨æ ·ä¾-PHP.php";
}else if ("e".endsWith(fileName)){
fileName="å·²ä¸çº¿çå¸åº.xls";
}else if ("f".endsWith(fileName)){
fileName="å
¨å½çå¸åºç 表.xls";
}else if ("g".endsWith(fileName)){
fileName="åä½æµç¨å¾.vsdx";
}else {
return null;
}
//è·åä¸è½½æ件路å¾
String filenamedownload=getServletContext().getRealPath("/webnew/common/"+fileName);
//转æ¢æ件åç¼ç
if (getRequest().getHeader("User-Agent").toLowerCase().indexOf("firefox")>0){
try {
fileName=new String(fileName.getBytes("UTF-8"),"iso-8859-1");
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
}else {
try{
fileName= URLEncoder.encode(fileName,"UTF-8");
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
}
InputStream fis=null;
try {
fis=new FileInputStream(filenamedownload);
}catch (FileNotFoundException e){
e.printStackTrace();
}
return fis;
}
public String fileDownload(){
System.out.println("fileDownload()============");
fileStream=download();
return SUCCESS;
}
public InputStream getFileStream() {
return fileStream;
}
public void setFileStream(InputStream fileStream) {
this.fileStream = fileStream;
}
public String getIndex() {
return index;
}
public void setIndex(String index) {
this.index = index;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
第äºæ¥ï¼é ç½®struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="openPlatform" extends="nsp" namespace="/openPlatform">
<action name="openPlatformMethod" class="OpenPlatformAction" method="openPlatformMethod">
<result name="success0">/openPlatform/openPlatform.jsp</result>
<result name="success1"></result>
<result name="success2"></result>
<result name="success3">/openPlatform/openPlatform_down.jsp</result>
</action>
<action name="fileDownload" class="OpenPlatformAction" method="fileDownload">
<result name="success" type="stream">
<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
<param name="inputName">fileStream</param>
<param name="contentDisposition">attachment;filename=${fileName}</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
ä¸:对äºstruts.xmlä¸å 个æ ç¾çä»ç»ï¼
contentType æå®ä¸è½½æ件çæ件类å ââ application/octet-stream 表示æ éå¶
inputName æµå¯¹è±¡å ââ æ¯å¦è¿éåinputStreamï¼å®å°±ä¼èªå¨å»æ¾Actionä¸çgetInputStreamæ¹æ³ã
contentDisposition 使ç¨ç»è¿è½¬ç çæ件åä½ä¸ºä¸è½½æ件å ââ é»è®¤æ ¼å¼æ¯attachment;filename="${fileName}"ï¼å°è°ç¨è¯¥Actionä¸çgetFileNameæ¹æ³ãå ¶ä¸ï¼attachment ï¼ä¸è½½æ¶ä¼æå¼ä¸è½½æ¡
bufferSize ä¸è½½æ件çç¼å²å¤§å°