【问题标题】:Problems with image upload From Android device to .NET webservice从 Android 设备到 .NET Web 服务的图像上传问题
【发布时间】:2014-07-31 08:14:26
【问题描述】:

已经好几天了,我仍然坚持这个图片上传的事情,我无法让它工作。我不会上传我的任何源代码,因为它看起来很可悲并且无法正常工作,所以我必须请求可以为我完成这项工作的人。我已经在谷歌上搜索了一段时间,但似乎无法让它们中的任何一个工作。

基本上我要做的是将位图上传到服务器,它必须作为 fileStream 发送,如下所示。就是这样。

无论如何,这里是 Web 服务的源代码。这将在 Android 设备发出 HttpPost 请求时触发。请注意,这不是我的代码。其他人负责这件事。

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;
   }

非常感谢任何帮助。

【问题讨论】:

    标签: android androidhttpclient


    【解决方案1】:

    我就是这样做的,你当然是在后台任务中运行

        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) {
    
        }
    

    【讨论】:

    • 如果文件是ImageView转位图怎么办?我应该如何处理文件路径?
    • 然后您需要将位图转换为 FileInputStream,类似于此 ByteArrayOutputStream stream = new ByteArrayOutputStream(); si.Image.compress(CompressFormat.JPEG, 100, 流); InputStream is = new ByteArrayInputStream(stream.toByteArray());
    • 参数[2]呢?它的内容是什么?
    • 我收到此错误 java.lang.NoClassDefFoundError: org.apache.commons.io.output.ByteArrayOutputStream 我在论坛中发现我应该将 commons-io 放入我的库中。它已经在图书馆里了,我该如何解决这个问题?
    • 好的,格式错误的网址已经完成。我如何获得 httpURLConnection 的响应?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 2013-03-22
    相关资源
    最近更新 更多