to implement a file upload, first add the page's from form to the
<form action= "upload" enctype= "Multipart/form-data" >
/**
* This method is a service, can also write a controller method, to see their hobbies and needs
* If this method is called from the controller, the Multipartfile object can use request.getfile (the Name property value of the file in the form) to get
*/
Public String upload (multipartfile partfile) {
String fileName = Partfile.getoriginalfilename (); Get file name
String Path = "D:\\file"; Set the upload path or upload to Linux
File File = new file (path+filename); Create an IO file object, combining the upload path and file name into a new path
If there is no file directory in the local D drive, you can create a directory using mkdir
if (file.getParentFile.exists ()) {//Getparentfile method is called first, the function is to get the parent directory; Exists method is to determine if the parent directory exists
Why do I need to call the method to get the parent directory when I create the directory here?
If you do not get the parent directory first, the file name is also created as a directory when you create it. Because when you create a file, the file name and address are combined in a
From For example: The upload path is "d:\\file\a.txt", if you do not first get the parent directory, then the created directory has file and A.txt, instead, the created directory is only the file
File.getParentFile.mkdirs (); If it does not exist, call the Mkdirs method to create the parent directory.
}
Uploading files
Partfile.transferto (file); An exception is thrown here
Return "File upload success";
}
Java_ File Upload