This article illustrates the method of struts2+jsp file uploading. Share to everyone for your reference. Specifically as follows:
1. Java Code:
Package com.wang.test;
Import Java.io.InputStream;
Import Org.apache.struts2.ServletActionContext;
Import com.opensymphony.xwork2.Action;
Import Com.opensymphony.xwork2.ActionSupport; The public class Downloadphonefile extends Actionsupport {////getdownloadfile () method must return a inputstream. The getResourceAsStream () method can stream the resource output public InputStream Getdownloadfile () {return servletactioncontext.getservle
Tcontext (). getResourceAsStream ("/upload/userlogin_7.27.apk");
Public String Execute () {return action.success; /************* "Implementation of Struts2 file downloads" *********************************************///If you write a link directly to the file you want to download, for some time , the default will be automatically opened in the browser//This situation is very detrimental to our file downloads and permissions control. Therefore, we do not implement the file download in this way//we are using the standard HTTP protocol approach, output binary stream, causing the browser to recognize the stream, it is the file download//In fact, this way is related to the output, when clicked on the download link, will produce a download of information. It is related to result//So go to Struts-default.xml view <result-type/> results type//where <result-type name= "stream" class= " Org.apache.struts2.dispatcher.StreamResult "/>//In fact, here the Streamresult class is specifically used to execute files under/************* "Every time the file is downloaded, the console prompts the socket exception" ***********************************///Error message is java.net.SocketException: Connection Reset by Peer:socket write error//download itself is also a socket operation, so throw the exception. In fact, this anomaly can be ignored. Each time the download, the exception will be thrown//throws exception on the Getdownloadfile () method, the console will not report this exception information/************* " Source code fragment for the Streamresult class used to process file downloads "********************************/// This shows the source snippet for the Org.apache.struts2.dispatcher.StreamResult class//public class Streamresult extends strutsresultsupport{/
/protected String contentType = "Text/plain";
protected String contentlength;
protected String contentdisposition = "inline";
protected String InputName = "InputStream";
protected InputStream InputStream;
protected int buffersize = 1024; /************* "On the three important attributes of Streamresult class" ******************************************/// Here we focus on the three attributes of the Streamresult class: ContentType, Contentdisposition, InputName// These properties are automatically injected into the object by Struts2 after the Struts.xml configuration, where contenttype is used to specify the downloaded fileThe type, contentdisposition is used to specify the name of the download file//other buffersize used to set the size of the buffer when downloading files, the default is 1KB, usually by the default 1KB on//actually these properties are entirely based on the HTTP protocol. The HTTP protocol specifies that when downloading files, these attributes are used//the most critical is the protected String InputName property, which is used to specify the IO stream of the file that is actually downloaded, so downloadaction must return an input stream. Because the download, itself is a file input from the server side of the Operation/********************************************************************************
*******/
}
2. The XML code is as follows:
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/dtds/ Struts-2.1.dtd ">
<struts>
<package name=" Default "extends=" Struts-default "namespace="/">
<action name= "Download" class= "Com.wang.test.DownLoadPhoneFile" > <result name= "Success"
Stream ">
<param name=" ContentType ">application/vnd.ms-powerpoint</param>
<param name=" Contentdisposition ">attachment;filename=" userlogin_7.27.apk "</param>
<param name=" InputName " >downloadFile</param>
</result>
</action>
</package>
</struts >
3. The JSP page code is as follows:
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<%
String Path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
I hope this article will help you with the JSP program design.