Struts use plugin jqueryupload upload files, background processing file code

Source: Internet
Author: User
Tags uuid

Package cn.axhu.education.action.file;

Import Cn.axhu.education.common.FileSystemConfig;
Import Cn.axhu.education.common.utils.FileUtils;
Import Net.ruixin.web.struts2.SimpleActionSupport;
Import org.apache.struts2.convention.annotation.Action;
Import Org.apache.struts2.convention.annotation.Namespace;
Import Org.apache.struts2.convention.annotation.ParentPackage;
Import Org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
Import Org.json.JSONObject;

Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.InputStream;
Import Java.io.PrintWriter;
Import java.util.*;

/**
* Created by yi.wang1 on 2015/3/9.
*/
@ParentPackage ("Default")
@Namespace ("/file")
public class Chapterattachuploadaction extends Simpleactionsupport {

@Action ("Saveattachfile")
public void Saveattachfile () {
PrintWriter out = null;
try {
Initialize variables
InputStream stream = null;
String Ret_filename = null;//returned to the front-end modified file name
map<string, object> result = new hashmap<string, object> ();
String Ret_filepath;
File UploadedFile;
String Uuid_filename;
try {
Set character sets
Request.setcharacterencoding ("UTF-8");
Response.setcontenttype ("text/html; Charset=utf-8 ");
out = Response.getwriter ();

File Save directory path
String Savepath = Filesystemconfig.getfilerootpath () + "chapterattach/";

Create a folder
File Dirfile = new file (Savepath);
if (!dirfile.exists ()) {
Dirfile.mkdirs ();
}
Get file stream
Multipartrequestwrapper Multiwrapper = (multipartrequestwrapper) request;
if (Multiwrapper.haserrors ()) {
for (Iterator erroriter = Multiwrapper.geterrors (). Iterator (); Erroriter.hasnext ();) {
String error = (string) erroriter.next ();
Log.error (Error);
}
}
File file = null;
Enumeration fileparameternames = Multiwrapper.getfileparameternames ();
while (fileparameternames! = null && fileparameternames.hasmoreelements ()) {
String inputname = (string) fileparameternames.nextelement ();
string[] ContentType = Multiwrapper.getcontenttypes (InputName);
if (contentType! = null) {
string[] FileName = Multiwrapper.getfilenames (InputName);
if (fileName! = null) {
Ret_filename = filename[0];
file[] files = multiwrapper.getfiles (InputName);
FILE = Files[0];
}
}
}
stream = new FileInputStream (file);

Initialize the filename and define the upload file
UUID uuid = Uuid.randomuuid ();
String suffix = ret_filename.substring (Ret_filename.lastindexof (".") + 1);
Uuid_filename = uuid.tostring () + "." + suffix;
Ret_filepath = Savepath + "/" + Uuid_filename + ". Temp";
UploadedFile = new File (Ret_filepath);
Fileutils.savefile (UploadedFile, stream);
} catch (Exception e) {
Read-write error exception handling
Result.put ("Message", E.getmessage ());
Out.print (new Jsonobject (Result). ToString ());
Return
} finally {
Resource Shutdown exception Handling
try {
if (stream! = null) {
Stream.Close ();
}
} catch (Exception e) {
Result.put ("Message", E.getmessage ());
Out.print (new Jsonobject (Result). ToString ());
Return
}
}
Result.put ("message", "success");
Result.put ("FileName", ret_filename);
Result.put ("FilePath", "chapterattach/" + uuid_filename + ". Temp");
Out.print (new Jsonobject (Result). ToString ());
} finally {
return information to
if (out! = null) {
Out.flush ();
Out.close ();
}
}
}

}

Struts use plugin jqueryupload upload files, background processing file code

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.