On the problem of multipartfile file in Java data dump __java

Source: Internet
Author: User

Error background: Because the file is stored on a third party server, all need to speak will receive the Multipartfile file to convert to file and then transfer. (Spring MVC)

The following two methods were found through the search engine.

are declared in the spring XML. As follows:

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

If you need to add the largest, minimum and other scope control, please own Baidu reference.

Method One: Strong turn

Method Two:

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

Parental test is effective. However, the problem with late discovery settings caused the file conversion to be incorrect and the file unreadable, causing the program to throw the is not a normal file exception.

Because of the randomness of the error, the option to use a buffer to implement this transformation is to use the Multipartfile.transferto () method with temporary files created in Java.

The code is as follows:

1 2 3 4 5 6 7 8 9 File f = null;      try {f=file.createtempfile ("tmp", NULL);         File.transferto (f);<br> f.deleteonexit ();  catch (HttpException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();}

Parental test is effective.

Related Article

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.