Android uploads images to ASP.

Source: Internet
Author: User

Recently in a mobile app to do a joint system management of the application, management procedures to manage the release of data and additions and deletions, the mobile app is responsible for displaying and manipulating business logic so a function.

The beginning of the road is very smooth, but to go through the Android client upload pictures to the ASP when there is a big problem, the picture is uploaded, is not displayed, with the photo Viewer to view the time display: ' No preview ', with the drawing software opened when the display ' Invalid bitmap file or file format not supported '!!!

People want to be sure that your code is a problem, OK, you see my code is what the problem:

StreamSR = context. Request.inputstream;byte[] buffer =New byte[4096];intbytesread = 0;//Writes the current data stream to the server-side folder ClientBinConst stringSavepath ="/risknoticeimg";//target picture pathstringDirpath = context. Server.MapPath (Savepath);stringPath = Dirpath +@"\" + Picname;using(FileStreamFS =File. Create (FilePath, 4096)) { while((Bytesread = Sr.) Read (buffer, 0, buffer. Length)) > 0) {//write information to the fileFs.               Write (buffer, 0, bytesread); }           }

Look, do you think there's a problem? I can not see, engaged for two days, people are almost collapsed, and then aimlessly in the Internet to find the answer, some say to Base64 code just line, but still no, not to say no, is I did not try, the most disgusting is this post: Click here to see, why say so, because obviously with me is the same problem, Obviously he also solved, but is not to tell you how to solve, helpless under the project manager, after his computer-like brain to solve the problem, the next is to witness the miracle of the moment:

Context. Request.files[0]. SaveAs (FilePath);

Yes, just this line of code is done, if you test this line of code is not possible, then you can according to its files.count to loop output its file like this: Context.request.files[i]. SaveAs (FilePath); This will solve the problem of uploading pictures can not be displayed, I hope that some people help it ^_^,

Finally, he was inspired by this document:

Package main;

Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.PrintWriter;
Import java.util.List;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.commons.fileupload.FileItem;
Import Org.apache.commons.fileupload.FileItemFactory;
Import Org.apache.commons.fileupload.disk.DiskFileItemFactory;
Import Org.apache.commons.fileupload.servlet.ServletFileUpload;

public class Hello extends httpservlet{
protected void Service (HttpServletRequest request,
HttpServletResponse response) throws Servletexception, IOException
{
Try
{
Request.setcharacterencoding ("UTF-8"); Set the encoding format for processing request parameters
Response.setcontenttype ("Text/html;charset=utf-8"); Setting the Content-type field value
PrintWriter out = Response.getwriter ();
The following code starts using the Commons-uploadfile component to process the uploaded file data
Fileitemfactory factory = new Diskfileitemfactory (); Creating Fileitemfactory Objects
Servletfileupload upload = new Servletfileupload (factory);
Parse the request and get the Fileitem object that uploaded the file
list<fileitem> items = upload.parserequest (request);
Get the path of the uploaded file from the parameters in the Web. xml file
String Uploadpath = "d:\\upload\\";
File File = new file (Uploadpath);
if (!file.exists ())
{
File.mkdir ();
}
String filename = ""; File name to save the uploaded files to the server
InputStream is = null; The InputStream object for the currently uploaded file
Cyclic processing of uploaded files
for (Fileitem Item:items)
{
Working with normal form fields
if (Item.isformfield ())
{
if (Item.getfieldname (). Equals ("filename"))
{
If the new file is not empty, save it in filename
if (!item.getstring (). Equals (""))
filename = item.getstring ("UTF-8");
}
}
Handling Upload Files
else if (item.getname () = null &&!item.getname (). Equals (""))
{
Intercepts file names from the path of uploaded files sent by the client.
filename = item.getname (). SUBSTRING (
Item.getname (). lastIndexOf ("\ \") + 1);
is = Item.getinputstream (); InputStream object to get uploaded files
}
}
Combine the path and upload file name into a full server-side path
filename = uploadpath + filename;
If the server already exists and uploads a file with the same name as the file, the output message
if (new File (filename). Exists ())
{
New File (filename). Delete ();
}
Start uploading files
if (!filename.equals (""))
{
Open the server upload file with FileOutputStream
FileOutputStream fos = new FileOutputStream (filename);
byte[] buffer = new byte[8192]; 8K bytes per read
int count = 0;
Begins to read the bytes of the uploaded file and outputs it to the server's upload file output stream
while ((count = is.read (buffer)) > 0)
{
Fos.write (buffer, 0, count); Writing a byte stream to a server-side file
}
Fos.close (); Close the FileOutputStream object
Is.close (); InputStream Object
System.out.println ("File Upload success!!! ");
Out.println ("File Upload succeeded!");
}
}
catch (Exception e)
{
SYSTEM.OUT.PRINTLN ("exception occurred");
E.printstacktrace ();
}
}

}

Android uploads images to ASP.

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.