On the problem of Multipartfile to file in Java data dump

Source: Internet
Author: User

Error background: Since the file is stored on a third-party server, all need to be told to convert the received Multipartfile file to files and then transfer. (Spring MVC)

The following two methods have been found through search engines.

are declared in the preceding spring XML. As follows:

<id= "Multipartresolver"  class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver "/>  

If you want to add the largest, minimum range control, please Baidu reference.

Method One: Strong turn

Method Two:

Commonsmultipartfile CF = (commonsmultipartfile) multfile;   Diskfileitem fi = (Diskfileitem) cf.getfileitem ();  File File = Fi.getstorelocation ();

The pro-test is effective. However, late-discovery Setup issues cause file conversion errors and the file is unreadable thereby causing the program to throw an is not a normal file exception.

because of the randomness of the error, choosing to use a buffer to implement the transformation is to use the temporary file created by Java Multipartfile.transferto () method.

The code is as follows:

File f = null;try {    f=file.createtempfile ("tmp", null);    File.transferto (f);
F.deleteonexit (); } catch (HttpException e) { e.printstacktrace ();} catch (IOException e) { e.printstacktrace ();}

The pro-test is effective.

On the problem of Multipartfile to file in Java data dump

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.