if (Stringutils.isnotempty (Picstorepath)) {
picstorepath=picstorepath+ "/";
}
}
public static void Deletezip (String realpath) {
Get file objects by path
File File = new file (Realpath);
Determine if a file exists
if (file.exists ()) {
File deletion
File.delete ();
System.out.println ("Delete succeeded" +realpath);
}
}
public static void DeleteFile (File f) {
For (File fi:f.listfiles ()) {
if (Fi.isdirectory ()) {
DeleteFile (FI);
}
else{
Fi.delete ();
}
}
F.delete ();
}
@RequestMapping ("/download")
public void Download (HttpServletRequest request, httpservletresponse response) {
try {
System.out.println ("Lai");
String Cusid = Request.getparameter ("cus_id");
String Applyid = Request.getparameter ("apply_id");
Folder path
String src= GetPath ("/web-inf/upload/pls/" +cusid+ "/" +applyid);
The copy will compress the file path
String Path = GetPath ("/web-inf/upload/pls/" +cusid+ "/" +applyid) + "1";
Copy a modified name compress download
Copydirectory.copy (src, path);
File file= new file (path);
Updatefilename.renamefile (file);
New Compactalgorithm (New File (path+)/. /", applyid+". zip ")). Zipfiles (file);
String filename = applyid+ ". zip";
Compressed file full
String Realpath = path+ "/". /"+applyid+". zip ";
InputStream fis = new Bufferedinputstream (new FileInputStream (Realpath));
Response.setheader ("Content-disposition", "attachment;filename=" +filename);
byte [] bts = new byte [cache_byte_length];
int len = 0;
OutputStream outputstream = new Bufferedoutputstream (Response.getoutputstream ());
while ((Len=fis.read (BTS, 0, Cache_byte_length))!=-1) {
Outputstream.write (BTS, 0, Len);
}
Outputstream.flush ();
Fis.close ();
Outputstream.close ();
DeleteFile (file);
Deletezip (Realpath);
} catch (Exception ex) {
Ex.printstacktrace ();
}
}
@RequestMapping (value = "/upload", method = Requestmethod.post)
public void Upload (@RequestParam (value = "file") Multipartfile file,
@RequestParam (value = "files") multipartfile[] files, httpservletrequest request,
HttpServletResponse response) throws Exception {
If you are uploading only one file, you only need to receive the file Multipartfile type, and you do not need to explicitly specify @requestparam annotations
If you want to upload multiple files, you will need to use the multipartfile[] type to receive the file, and also specify the @requestparam annotation
And when uploading multiple files, all <input in the foreground form
type= "file"/> name should be myfiles, otherwise the myfiles in the parameter can not get all the uploaded files
enctype = "Multipart/form-data"
Map<string, object> context = new hashmap<> (5);
list<string> fileNames = new arraylist<> ();
for (int i=0;i<files.length;i++) {
Filenames.add (UploadFile (i,files[i], request, response));
}
String fileext = "."
+ e.getoriginalfilename (). substring (E.getoriginalfilename (). LastIndexOf (".") + 1). toLowerCase ();
if (! Arrays.<string>aslist (Allowfileext.split (COMMA)). Contains (Fileext)) {
Sendfailuremessage (response, "The upload file extension is not allowed. \ n only allow "+ Allowfileext +" format. ");
return null;
}
String dir=request.getparameter ("dir");
String picname=request.getparameter ("Picname");
if (i!=0) {
Picname=picname+i;
}
String Realpath = GetPath ("/web-inf/upload/" +picstorepath+dir);
There is no need to deal with the problem of Io stream shutdown, because the Fileutils.copyinputstreamtofile () method will automatically shut down the IO stream used, I see it's source code to know
File Filedest = new file (Realpath);
If the destination folder does not exist
if (!filedest.isdirectory () | |!filedest.exists ()) {
if (!filedest.mkdirs ()) {
Log.debug ("Directory folder not saved, failed when creating destination folder!");
return null;
}
}
File Srcfile = new file (Realpath, picname+savedfileext);//e.getoriginalfilename () +fileext
E.getoriginalfilename () +fileext
File Srcfile = new file (Realpath, picname+fileext);
System.out.println (Srcfile.getabsolutepath ());
E.transferto (Srcfile);
return Srcfile.getname ();
}
return null;
}
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.