In the Web project, file upload, avatar Upload such functions are often used, the following struts2 in the implementation of the file upload function as an example, a simple geographic file upload function encoding ideas.
Project directory Structure
Project Source Code
Web. XML
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "3.0" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "> <filter> <filter-name>struts 2</ Filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts 2</filter-name> < url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> < welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
Struts.xml
<span style= "FONT-SIZE:12PX;" ><?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=" Default "extends=" Struts-default "> <action name=" upload "class=" com.upload.action.UploadAction "method=" Upload "> <result name= "Success" >/success.jsp</result> <result name= "error" >/upload.jsp</result > </action> </package></struts></span>
Uploadaction.java
Package Com.upload.action;import Java.io.file;import Java.io.fileinputstream;import java.io.FileOutputStream; Import Java.io.inputstream;import java.io.outputstream;import java.util.uuid;import javax.servlet.ServletContext; Import Org.apache.struts2.servletactioncontext;import Com.opensymphony.xwork2.actioncontext;import Com.opensymphony.xwork2.actionsupport;public class Uploadaction extends actionsupport{private File upload; Upload file private String uploadfilename; Uploaded file name public file Getupload () {return upload;} public void Setupload (File upload) {this.upload = upload;} Public String Getuploadfilename () {return uploadfilename;} public void Setuploadfilename (String uploadfilename) {this.uploadfilename = Uploadfilename;} Public String upload () throws exception{//if the upload function is selected, the upload operation is performed; otherwise, nothing is done if (getupload () = null) {// According to the uploaded file to get the input stream InputStream is = new FileInputStream (Getupload ());//Change the upload file name: Random number + Upload file name Setuploadfilename ( Uuid.randomuuid (). toString () + getuploadfilename ());//Specify the address of the output stream, here is the output to the serviceOutputStream OS = new FileOutputStream (Getwebrootpath () + "images\\userphoto\\" + Images/userphoto under the root directory of the service project Getuploadfilename ()); byte buffer[] = new Byte[1024];int count = 0;//writes the file to a file in the specified location while ((count = is.read (buffer)) > 0) { Os.write (buffer, 0, count);} Closes the output stream object Os.close ();//closes the input stream object Is.close ();//returns return SUCCESS;} else {return ERROR;}} /** * get Web Project root directory */public String Getwebrootpath () throws Exception {Actioncontext Actioncontext = Actioncontext.getcontex T (); ServletContext ServletContext = (ServletContext) actioncontext.get (Servletactioncontext.servlet_context); String RootPath = Servletcontext.getrealpath ("/"); return rootpath;}} <strong></strong>
upload.jsp
<span style= "FONT-SIZE:12PX;" ><%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%@ taglib uri= "/struts-tags" prefix = "s"%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >success.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
The above method can be used not only for uploading image types, but also for uploading other file types.
Note: The default size limit of the Struts 2 upload file is 2MB, so the file should not be too large when testing, it is best to find a small file. If you want to modify the default size, you only need to modify struts.multipart.maxSize values in the struts.properties or struts.xml of struts 2, such as struts.multipart.maxsize= 1024 indicates that the total size of the uploaded file cannot exceed 1KB.
If the size of the uploaded file in Struts.xml is not more than 50MB, you can add it in the <struts> </struts> tag of Struts.xml:
<constant name= "struts.multipart.maxSize" value= "51200"/>
File Upload function in Struts2