Springmvc upload files using jqueryupload, background processing method

Source: Internet
Author: User

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

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.