0

It's been days and I am still stuck with this image upload thing and I can't get this to work. I will not be uploading any of my source code since it looks pathetic and not working so I have to beg someone who can make this work for me. I've been Googling around for a while but can't seem to make any of them work.

Basically what I am trying to do is upload a Bitmap to the server and it has to be sent as fileStream as you can see below. That's all.

Here is the source code for the web service anyway. This will be fired up when HttpPost request is made from the Android device. Take note that this is not my code. Someone else is in charge of this.

public Stream FileUpload(string fileName, Stream fileStream){
       var serverPath = System.Web.Hosting.HostingEnvironment.MapPath("~/FileUpload/");
       if (File.Exists(serverPath + fileName)) File.Delete(serverPath + fileName); // delete file if already used


       //FileStream fileToupload = new FileStream("D:\\FileUpload\\" + fileName, FileMode.Create);
       FileStream fileToupload = new FileStream(serverPath + fileName, FileMode.Create);

       byte[] bytearray = new byte[10000];//
       int bytesRead, totalBytesRead = 0;
       do
       {
           bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
           totalBytesRead += bytesRead;
       } while (bytesRead > 0);

       fileToupload.Write(bytearray, 0, bytearray.Length);
       fileToupload.Close();
       fileToupload.Dispose();

       FileStream fs = File.OpenRead(serverPath + fileName);
       WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
       return fs;
   }

Any help is much appreciated.

1 Answer 1

1

This is how I do it, that you of course run in a background task

    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "***1234321**";
    String filePath = "path/to/file";
    String fileName = new File(filePath).getName();
    StringBuffer response =new StringBuffer();

    try {
        URL connectUrl = new URL((String) params[0]);
        FileInputStream fileInputStream = new FileInputStream(filePath);
        HttpURLConnection conn = (HttpURLConnection) connectUrl.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\""+ params[2] +"\"; filename=\"" + fileName +"\"" + lineEnd);
        dos.writeBytes("Content-Type: application/octet-stream" + lineEnd);
        dos.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
        dos.writeBytes(lineEnd);

        // create a buffer of maximum size
        int bytesAvailable = fileInputStream.available();
        int maxBufferSize = 1024;
        int bufferSize = Math.min(bytesAvailable, maxBufferSize);
        byte[] buffer = new byte[bufferSize];

        // read file and write it into form...
        int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        while (bytesRead > 0) {
            dos.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        // send multipart form data necessary after file data...
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // close streams
        fileInputStream.close();
        dos.flush();


        dos.close();

    } catch (Exception e) {

    }
Sign up to request clarification or add additional context in comments.

6 Comments

What if the file is an ImageView converted to bitmap? What should I do with the filePath?
Then you will need to convert the bitmap to a FileInputStream, something similar to this ByteArrayOutputStream stream = new ByteArrayOutputStream(); si.Image.compress(CompressFormat.JPEG, 100, stream); InputStream is = new ByteArrayInputStream(stream.toByteArray());
what about the params[2]? what would be the content of it?
I'm getting this error java.lang.NoClassDefFoundError: org.apache.commons.io.output.ByteArrayOutputStream I found in the forums that I should put the commons-io in my library. It's already in the library how do I fix this?
Okay the malformed url is done. How do i get the response of the httpURLConnection?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.