Springmvc+volley Implement Android Upload file

Source: Internet
Author: User


The Android side uses the volley request base class to implement several of its methods to upload files in a mosaic form



The server side uses the spring framework to receive files.



One, Android: three categories: Mainactivity,postrequest,uploadfilerequest.



1.PostRequest inherits from Request<string>, realizes its deliverresponse and Parsenetworkresponse as well as getparams three methods,



Constructor is

Public Postrequest (String url,map<string,string>params, Responselistener Listener) {...}

2.UploadFileRequest inherits from Postrequest, implements its GetBody () and Getbodycontenttype () methods, getbody to splice form data, Getbodycontenttype is used to return the form type:


Return "multipart/form-data;boundary=------8d74g45h";
Constructor is

Public uploadfilerequest (String URL, list<fileform>filelist, Responselistener Listener) {...}

3. Call Uploadfilerequest in mainactivity and add it to the volley queue.


@Controller public
class Uploadcontroller {

    @RequestMapping ("Uploadimage")
    //@ResponseBody
    Public void AddImage (HttpServletRequest request) throws IllegalStateException, ioexception{
        Multiparthttpservletrequest multiparthttpservletrequest = (multiparthttpservletrequest) request;
        Multipartfile file = Multiparthttpservletrequest.getfile ("UploadFile");
        if (!file.getoriginalfilename () ==null) {
            file TargetFile = new file ("F:/789.png");
            File.transferto (targetfile);
        }



Second, the server side:

When using Multiparthttpservletrequest, remember to add the multipart parser to the spring configuration file:
@Controller public
class Uploadcontroller {

    @RequestMapping ("Uploadimage")
    //@ResponseBody
    Public void AddImage (HttpServletRequest request) throws IllegalStateException, ioexception{
        Multiparthttpservletrequest multiparthttpservletrequest = (multiparthttpservletrequest) request;
        Multipartfile file = Multiparthttpservletrequest.getfile ("UploadFile");
        if (!file.getoriginalfilename () ==null) {
            file TargetFile = new file ("F:/789.png");
            File.transferto (targetfile);
        } 
This completes the file upload operation.
Reference:
1.Spring File Upload processing: http://amcucn.iteye.com/blog/264457
2.Volley File Upload operation: http://blog.csdn.net/jxxfzgy/article/details/44064481




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.