Java_ File Upload

Source: Internet
Author: User

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

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.