APP programming in Android-Use HTTP to upload files

Source: Internet
Author: User

 

[Author] Zeng jiansheng

[Author mailbox] zengjiansheng1@126.com

[Author's QQ] 190678908

Author MSN zengjiansheng1@hotmail.com

[Author's blog] blog.csdn.net/newjueqi

 

 

**************************************** ***************************************

In Android client programming (especially SNS clients), you often need to implement the registration function Activity. You need to enter the user name, password, email address, and photo before registering. However, there is a problem in this case. You can use form in HTML to implement the above registry form. The required information will be automatically encapsulated as a complete HTTP protocol, but in Android, how do I encapsulate these parameters and files to be uploaded as HTTP?

Let's first try to see what information the form encapsulates.

 

Step 1: Compile a Servlet and save the received HTTP information in a file. The Code is as follows:

 

Public VoidDoPost (HttpServletRequest request, HttpServletResponse response)

ThrowsServletException, IOException {

 

// Obtain the input stream, which is the entity content in the HTTP protocol.

ServletInputStream sis = request. getInputStream ();

// Buffer zone

ByteBuffer [] =New Byte[1024];

FileOutputStream fos =NewFileOutputStream ("d: // file. log ");

IntLen = sis. read (buffer, 0, 1024 );

// Cyclically read the information in the stream into the file. log file.

While(Len! =-1)

{

Fos. write (buffer, 0, len );

Len = sis. readLine (buffer, 0, 1024 );

}

Fos. close ();

Sis. close ();

}

 

Step 2: Implement a form page such as 1, generate a registry ticket, and submit it to the Servlet

Figure 1

 

 

 

 

The detailed code is as follows:

 

<Form action = "servlet/ReceiveFile" method = "post" enctype = "multipart/form-data">

First parameter <input type = "text" name = "name1"/> <br/>

The second parameter <input type = "text" name = "name2"/> <br/>

The first uploaded file <input type = "file" name = "file1"/> <br/>

The second uploaded file <input type = "file" name = "file2"/> <br/>

<Input type = "submit" value = "submit">

</Form>

 

Note: To upload an attachment, you must set enctype to multipart/form-data to upload the attachment.

 

Step 3: After entering the information, click "Submit" and open the file. log file in drive d with notepad. The data is as follows:

----------------------------- 7d92221b604bc

Content-Disposition: form-data; name = "name1"

 

Hello

----------------------------- 7d92221b604bc

Content-Disposition: form-data; name = "name2"

 

World

----------------------------- 7d92221b604bc

Content-Disposition: form-data; name = "file1"; filename = "C:/2.GIF"

Content-Type: image/gif

 

GIF89a

 

Why?

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.