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