struts实现上传下载
上传页面的实现代码如下
upload.jsp
<%@pagelanguage="java"pageEncoding="UTF-8"%>
<%@tagliburi="http://struts.apache.org/tags-bean"prefix="bean"%>
<%@tagliburi="http://struts.apache.org/tags-html"prefix="html"%>
<%@tagliburi="http://struts.apache.org/tags-logic"prefix="logic"%>
<%@tagliburi="http://struts.apache.org/tags-tiles"prefix="tiles"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html:htmllang="true">
<head>
<html:base/>
<title>upload.jsp</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
<html:formaction=""method="post"enctype="multipart/form-data">
<table>
<tr>
<tdalign="center"colspan="2">
<fontsize="4">PleaseEntertheFollowingDetails</font>
</td>
</tr>
<tr>
<tdalign="left"colspan="2">
<fontcolor="red"><htm:errors/></font>
</td>
</tr>
<tr>
<tdalign="right">
FileName
</td>
<tdalign="left">
<html:fileproperty="theFile"></html:file>
</td>
</tr>
<tr>
<tdalign="center"colspan="2">
<html:submit>UploadFile</html:submit>
</td>
</tr>
</table>
</html:form>
</body>
</html:html>
其中struts通过表单实现页面的上传时,需要在form表单把enctype="multipart/form-data",使用了这个属性后,HttpServletRequest对象的getParameter()方法就无法直接得到用户所提交的其他数据。好在struts对这种格式编码的数据进行了处理,使得用户提交的数据仍然可以被直接设置成actionForm种对应的值。
ActionForm:
packageActionForm;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.upload.FormFile;
publicclassUploadFormextendsActionForm{
privateFormFiletheFile;
publicFormFilegetTheFile(){
returntheFile;
}
publicvoidsetTheFile(FormFiletheFile){
this.theFile=theFile;
}
}
UploadAction文件
packagecom.xpjjy.struts;
importjava.io.File;
importjava.io.FileOutputStream;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
importorg.apache.struts.action.Action;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionForward;
importorg.apache.struts.action.ActionMapping;
importorg.apache.struts.upload.FormFile;
importActionForm.UploadForm;
publicclassUploadActionextendsAction{
publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
throwsException{
UploadFormuploadForm=(UploadForm)form;
FormFilefile=uploadForm.getTheFile();
StringcontentType=file.getContentType();
StringfileName=file.getFileName();
intfileSize=file.getFileSize();
byte[]fileData=file.getFileData();
System.out.println("Contentype:"+contentType);
System.out.println("FileName:"+fileName);
System.out.println("FileSize"+fileSize);
FileOutputStreamout=newFileOutputStream(newFile("D:\\"+fileName));
out.write(fileData);
out.close();
HttpSessionsession=request.getSession();
session.setAttribute("contentType",contentType);
session.setAttribute("fileName",fileName);
session.setAttribute("fileSize",Integer.valueOf(fileSize));
returnmapping.findForward("download");
}
}
httpSession的相关资料空间有
download.jsp
<%@pagelanguage="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%@tagliburi="http://struts.apache.org/tags-html"prefix="html"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
<title>DownloadandOpentheFile</title>
</head>
<body>
<html:linkaction="/download">Download</html:link>
<br>
<html:linkaction="/open">Open</html:link>
</body>
</html>
DownloadFileAction
packagecom.xpjjy.struts;
importjava.io.File;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionMapping;
importorg.apache.struts.actions.DownloadAction;
publicclassDownloadFileActionextendsDownloadAction{
protectedStreamInfogetStreamInfo(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)throwsException{
HttpSessionsession=request.getSession();
StringcontentType=(String)session.getAttribute("contentType");
StringfileName=(String)session.getAttribute("fileName");
response.setHeader("content-disposition","attachment;filename="+fileName);
Filefile=newFile("D:\\"+fileName);
returnnewFileStreamInfo(contentType,file);
}
}
Strut的配置文件struts-config.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstruts-configPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.3//EN""http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-beanname="uploadForm"type="ActionForm.UploadForm"/>
</form-beans>
<global-exceptions/>
<global-forwards/>
<action-mappings>
<actionpath="/OutputXMLAction"
type="com.xpjjy.struts.OutputXMLAction"
scope="request"
>
<forwardname="success"path="/index.jsp"/>
</action>
<action
attribute="uploadForm"
input="/upload.jsp"
name="uploadForm"
path="/upload"
scope="request"
type="com.xpjjy.struts.UploadAction"
>
<forwardname="download"path="/download.jsp"/>
</action>
<actionpath="/download"
type="com.xpjjy.struts.DownloadFileAction"
scope="request"
>
</action>
</action-mappings>
<message-resourcesparameter="com.xpjjy.struts.ApplicationResources"/>
</struts-config>