Struts2 Uploading Files

Source: Internet
Author: User

Xml:

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app id= "Webapp_9" version= "2.4"
Xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

Struts.xml

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Struts Public
"-//apache software foundation//dtd Struts Configuration 2.0//en"
"Http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts>
<constant name= "struts.multipart.maxSize" value= "20971520000" ></constant>
<package name= "Default" namespace= "/" extends= "Json-default" >
<action name= "uploadaction" class= "Com.huawei.s2.action.UploadAction"/>
<action name= "uploadbatchaction" class= "Com.huawei.s2.action.UploadBatchAction"/>
</package>
</struts>

INDEX.JSP:

<%@ 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" >
<base href= "<%=basePath%>" >
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8" >
<title>this is my JSP page</title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This is my page" >
<script src= "Js/jquery-1.8.3.js" ></script>
<body>
<form action= "Uploadaction!uploadfile" enctype= "Multipart/form-data" method= "POST" >
<input name= "uname" value= "Zhang San"/><br>
<input type= "File" name= "file"/><br>
<input type= "Submit" value= "Submission" >
</form>
</body>

INDEX2.JSP:

<%@ 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" >
<base href= "<%=basePath%>" >
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8" >
<title>this is my JSP page</title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This is my page" >
<script src= "Js/jquery-1.8.3.js" ></script>
<body>
<form action= "Uploadbatchaction!uploadfile" enctype= "Multipart/form-data" method= "POST" >
<input name= "uname" value= "Zhang San"/><br>
<fieldset>
<legend><input type= "button" value= "Add"/></legend>
<div><input type= "file" name= "Files"/><input type= "button" value= "Delete" ></div>
<div><input type= "file" name= "Files"/><input type= "button" value= "Delete" ></div>
<div><input type= "file" name= "Files"/><input type= "button" value= "Delete" ></div>
</fieldset>
<input type= "Submit" value= "Submission" >
</form>
</body>

Uploadaction:

Package com.huawei.s2.action;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import Java.util.UUID;
Import Org.apache.commons.io.FileUtils;
Import Org.apache.struts2.ServletActionContext;

public class Uploadaction {
Private String uname;
Private file file;//File Type property receive upload files property name must match form field file name
Private String filefilename;//to get the file name of the uploaded file, the property name must write: The name value of the File form field +filename
public void UploadFile () throws exception{
System.out.println (uname+ "=========" +filefilename);
String Realpath =servletactioncontext.getservletcontext (). Getrealpath ("/resources");
Fileutils.copyfile (file, new file (realpath+ "/" +uuid.randomuuid (). toString () + "_" +filefilename));
/*bufferedinputstream bis = new Bufferedinputstream (new FileInputStream (file));
Bufferedoutputstream BOS = new Bufferedoutputstream (New FileOutputStream (realpath+ "/" +filefilename));
int temp =-1;
while ((Temp=bis.read ())!=-1) {
Bos.write (temp);
}
Bos.flush ();
Bos.close ();
Bis.close (); */
}
Public String Getuname () {
return uname;
}
public void Setuname (String uname) {
This.uname = uname;
}
Public File GetFile () {
return file;
}
public void Setfile (file file) {
This.file = file;
}
Public String Getfilefilename () {
return filefilename;
}
public void Setfilefilename (String filefilename) {
This.filefilename = Filefilename;
}
}

Uploadbatchaction:

Package com.huawei.s2.action;

Import Java.io.File;
Import Java.util.Arrays;
Import Java.util.UUID;

Import Org.apache.commons.io.FileUtils;
Import Org.apache.struts2.ServletActionContext;

public class Uploadbatchaction {
Private String uname;
Private file[] files;//file Type property receive upload files property name must match form field file name
Private string[] filesfilename;//to get the file name of the uploaded file, the property name must be written like this: the name value of the File form field +filename
public void UploadFile () throws exception{
System.out.println (uname+ "=========" +arrays.tostring (filesfilename));
String Realpath =servletactioncontext.getservletcontext (). Getrealpath ("/resources");
for (int i=0;i<files.length;i++) {
Fileutils.copyfile (Files[i], new File (realpath+ "/" +uuid.randomuuid (). toString () + "_" +filesfilename[i]));
}
}
Public String Getuname () {
return uname;
}
public void Setuname (String uname) {
This.uname = uname;
}
Public file[] GetFiles () {
return files;
}
public void Setfiles (file[] files) {
This.files = files;
}
Public string[] Getfilesfilename () {
return filesfilename;
}
public void Setfilesfilename (string[] filesfilename) {
This.filesfilename = Filesfilename;
}
}

Struts2 Uploading Files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.