upload.jsp
<%@ page contentType="text/html;charset=GBK" language="java"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>Struts File Upload Example</title>
</head>
<body bgcolor="white">
<html:form action="/upload" method="post" enctype="multipart/form-data">
<table>
<tr>
<td align="center" colspan="2">
<font size="4">Please Enter the Following Details</font>
</td>
</tr>
<tr>
<td align="left" colspan="2">
<font color="red"><html:errors /></font>
</td>
</tr>
<tr>
<td align="right">
File Name
</td>
<td align="left">
<html:file property="theFile" />
</td>
</tr>
<tr>
<td align="center" colspan="2">
<html:submit>Upload File</html:submit>
</td>
</tr>
</table>
</html:form>
</body>
</html>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="uploadForm" type="cn.hxex.action.struts.form.UploadForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="uploadForm"
input="/upload.jsp"
name="uploadForm"
path="/upload"
scope="request"
type="cn.hxex.action.struts.action.UploadAction"
validate="false">
<forward name="download" path="/download.jsp" />
</action>
<action path="/download" type="cn.hxex.action.struts.action.DownloadFileAction" />
<action path="/open" type="cn.hxex.action.struts.action.OpenFileAction" />
</action-mappings>
<message-resources parameter="cn.hxex.action.struts.ApplicationResources" />
</struts-config>
UploadAction.java
package cn.hxex.action.struts.action;
import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import cn.hxex.action.struts.form.UploadForm;
public class UploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UploadForm uploadForm = (UploadForm) form;
// Process the FormFile
FormFile myFile = uploadForm.getTheFile();
String contentType = myFile.getContentType();
String fileName = myFile.getFileName();
int fileSize = myFile.getFileSize();
byte[] fileData = myFile.getFileData();
System.out.println("contentType: " + contentType);
System.out.println("File Name: " + fileName);
System.out.println("File Size: " + fileSize);
FileOutputStream out = new FileOutputStream(new File("c:\\" + fileName));
out.write(fileData);
out.close();
HttpSession session = request.getSession();
session.setAttribute("contentType", contentType);
session.setAttribute("fileName", fileName);
session.setAttribute("fileSize", Integer.valueOf(fileSize));
return mapping.findForward("download");
}
}
download.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>Download and Open the file</title>
</head>
<body>
<html:link action="/download">Download</html:link><br>
<html:link action="/open">Open</html:link>
</body>
</html>
DownloadFileAction.java
package cn.hxex.action.struts.action;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;
public class DownloadFileAction extends DownloadAction {
protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
String contentType = (String) session.getAttribute("contentType");
String fileName = (String) session.getAttribute("fileName");
// Set the content disposition
response.setHeader("Content-disposition", "attachment; filename="
+ fileName);
File file = new File( "c:\\" + fileName);
return new FileStreamInfo(contentType, file);
}
}