public void Importidentifyschemefile (Integer id,integer type,httpservletrequest request, httpservletresponse response ){
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> ();
Skillidentifyfile skillidentifyfile = new Skillidentifyfile ();
String filesrc;
String FileName;
File uploadedfile = null;
try {
Set character sets
Request.setcharacterencoding ("UTF-8");
Response.setcontenttype ("text/html; Charset=utf-8 ");
out = Response.getwriter ();
if (id==null) {
Result.put ("message", "scheme does not exist, cannot be uploaded");
Out.print (new Jsonobject (Result). ToString ());
Return
}
Get file stream
Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
map<string, multipartfile> filemap = Multipartrequest.getfilemap ();
Multipartfile multipartfile = null;
For (map.entry<string, multipartfile> set:fileMap.entrySet ()) {
Multipartfile = Set.getvalue ();//File name
}
stream = Multipartfile.getinputstream ();
Initialize the filename and define the upload file
UUID uuid = Uuid.randomuuid ();
filename = Multipartfile.getoriginalfilename ();//Original file name
String suffix = filename.substring (Filename.indexof (".") + 1);
Ret_filename = uuid.tostring () + "." + suffix;
File Save directory path
String Savepath = Filesystemconfig.getfilerootpath () + File_path;
Create a folder
File Dirfile = new file (Savepath);
if (!dirfile.exists ()) {
Dirfile.mkdirs ();
}
FILESRC = Savepath + File.separator + ret_filename;
UploadedFile = new File (FILESRC);
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
}
}
Initialize Scheme attachment properties
Skillidentifyfile.setfile (File_path + ret_filename);//download path
Skillidentifyfile.setname (filename);//Original file name
Skillidentifyfile.setupdatetime (New Date (System.currenttimemillis ()));
Skillidentifyfile.setskillidentifyid (ID);
Skillidentifyfile.settype (type);
Save current attachment to database
Skillidentifyservice.addskillidentifyfile (Skillidentifyfile);
Result.put ("message", file_success);
Result.put ("filename", filename);
Out.print (new Jsonobject (Result). ToString ());
} finally {
return information to
if (out! = null) {
Out.flush ();
Out.close ();
}
}
}
Springmvc upload files using jqueryupload, background processing method