0

我想上传图片及其信息。我想为 android 使用multipart/form data json。我也看过这个 Stack Overflow 问题Android:How to upload .mp3 file and image to http server?

但这对我不起作用。

这是我的代码:

        protected void doFileUpload(){
            Intent i = getIntent();
            String path = i.getStringExtra("pathimage");
            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            DataInputStream inStream = null;
            String existingFileName = path;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary =  "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1*1024*1024;
            String responseFromServer = "";
            String urlString = Constants.url_create_store;
              try
              {
               //------------------ CLIENT REQUEST
              FileInputStream fileInputStream = new FileInputStream(new File(existingFileName) );
               // open a URL connection to the Servlet
               URL url = new URL(urlString);
               // Open a HTTP connection to the URL
               conn = (HttpURLConnection) url.openConnection();
               // Allow Inputs
               conn.setDoInput(true);
               // Allow Outputs
               conn.setDoOutput(true);
               // Don't use a cached copy.
               conn.setUseCaches(false);
               // Use a post method.
               conn.setRequestMethod("POST");
               conn.setRequestProperty("Connection", "Keep-Alive");
               conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
               dos = new DataOutputStream( conn.getOutputStream() );
               dos.writeBytes(twoHyphens + boundary + lineEnd);
               dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd);
               dos.writeBytes(lineEnd);
               // create a buffer of maximum size
               bytesAvailable = fileInputStream.available();
               bufferSize = Math.min(bytesAvailable, maxBufferSize);
               buffer = new byte[bufferSize];
               // read file and write it into form...
               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 necesssary after file data...
               dos.writeBytes(lineEnd);
               dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
               // close streams
               Log.e("Debug","File is written");
               fileInputStream.close();
               dos.flush();
               dos.close();
              }
              catch (MalformedURLException ex)
              {
                   Log.e("Debug", "error: " + ex.getMessage(), ex);
              }
              catch (IOException ioe)
              {
                   Log.e("Debug", "error: " + ioe.getMessage(), ioe);
              }
              //------------------ read the SERVER RESPONSE
              try {
                    inStream = new DataInputStream ( conn.getInputStream() );
                    String str;

                    while (( str = inStream.readLine()) != null)
                    {
                         Log.e("Debug","Server Response "+str);
                    }
                    inStream.close();
              }
              catch (IOException ioex){
                   Log.e("Debug", "error: " + ioex.getMessage(), ioex);
              }
            }

 }

这是我的日志:

10-03 11:17:07.782: DEBUG/dalvikvm(119): GC_EXTERNAL_ALLOC freed 706 objects / 34184 bytes in 839ms
10-03 11:17:13.052: INFO/ActivityManager(67): Starting activity: Intent { cmp=shoop3.android.edu/.Activity3 (has extras) }
10-03 11:17:13.732: DEBUG/dalvikvm(468): GC_EXTERNAL_ALLOC freed 1652 objects / 91728 bytes in 187ms
10-03 11:17:15.162: INFO/ActivityManager(67): Displayed activity shoop3.android.edu/.Activity3: 1929 ms (total 1929 ms)
10-03 11:17:20.942: ERROR/Debug(468): File is written
10-03 11:17:24.802: ERROR/Debug(468): Server Response {"status":1}
10-03 11:17:25.272: ERROR/JSON Parser(468): Error parsing data org.json.JSONException: End of input at character 0 of 
10-03 11:17:25.282: DEBUG/Create Response(468): {"status":1}
10-03 11:17:25.312: INFO/ActivityManager(67): Starting activity: Intent { cmp=shoop3.android.edu/.shoop3Activity }
10-03 11:17:26.825: INFO/ActivityManager(67): Displayed activity shoop3.android.edu/.shoop3Activity: 1349 ms (total 1349 ms)
10-03 11:19:24.372: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
4

1 回答 1

3

您可以使用此 SimpleMultipartEntity 类来发送 Multipart 数据:

public class SimpleMultipartEntity implements HttpEntity {

private final static char[] MULTIPART_CHARS = "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        .toCharArray();

private String boundary = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
boolean isSetLast = false;
boolean isSetFirst = false;

public SimpleMultipartEntity() {
    final StringBuffer buf = new StringBuffer();
    final Random rand = new Random();
    for (int i = 0; i < 30; i++) {
        buf.append(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]);
    }
    this.boundary = buf.toString();

}

public void writeFirstBoundaryIfNeeds() {
    if (!isSetFirst) {
        try {
            out.write(("--" + boundary + "\r\n").getBytes());
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
    isSetFirst = true;
}

public void writeLastBoundaryIfNeeds() {
    if (isSetLast) {
        return;
    }
    try {
        out.write(("\r\n--" + boundary + "--\r\n").getBytes());
    } catch (final IOException e) {
        e.printStackTrace();
    }
    isSetLast = true;
}

public void addPart(final String key, final String value) {
    writeFirstBoundaryIfNeeds();
    try {
        out.write(("Content-Disposition: form-data; name=\"" + key + "\"\r\n")
                .getBytes());
        out.write("Content-Type: text/plain; charset=UTF-8\r\n".getBytes());
        out.write("Content-Transfer-Encoding: 8bit\r\n\r\n".getBytes());
        out.write(value.getBytes());
        out.write(("\r\n--" + boundary + "\r\n").getBytes());
    } catch (final IOException e) {
        e.printStackTrace();
    }
}

public void addPart(final String key, final String fileName,
        final InputStream fin) {
    addPart(key, fileName, fin, "application/octet-stream");
}

public void addPart(final String key, final String fileName,
        final InputStream fin, String type) {
    writeFirstBoundaryIfNeeds();
    try {
        type = "Content-Type: " + type + "\r\n";
        out.write(("Content-Disposition: form-data; name=\"" + key
                + "\"; filename=\"" + fileName + "\"\r\n").getBytes());
        out.write(type.getBytes());
        out.write("Content-Transfer-Encoding: binary\r\n\r\n".getBytes());

        final byte[] tmp = new byte[4096];
        int l = 0;
        while ((l = fin.read(tmp)) != -1) {
            out.write(tmp, 0, l);
        }
        out.flush();
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fin.close();
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
}

public void addPart(final String key, final File value) {
    try {
        addPart(key, value.getName(), new FileInputStream(value));
    } catch (final FileNotFoundException e) {
        e.printStackTrace();
    }
}

public String getBoundary() {
    return boundary;
}

public void setBoundary(String boundary) {
    this.boundary = boundary;
}

@Override
public long getContentLength() {
    writeLastBoundaryIfNeeds();
    return out.toByteArray().length;
}

@Override
public Header getContentType() {
    return new BasicHeader("Content-Type", "multipart/form-data; boundary="
            + boundary);
}

@Override
public boolean isChunked() {
    return false;
}

@Override
public boolean isRepeatable() {
    return false;
}

@Override
public boolean isStreaming() {
    return false;
}

@Override
public void writeTo(final OutputStream outstream) throws IOException {
    outstream.write(out.toByteArray());
}

@Override
public Header getContentEncoding() {
    return null;
}

@Override
public void consumeContent() throws IOException,
        UnsupportedOperationException {
    if (isStreaming()) {
        throw new UnsupportedOperationException(
                "Streaming entity does not implement #consumeContent()");
    }
}

@Override
public InputStream getContent() throws IOException,
        UnsupportedOperationException {
    return new ByteArrayInputStream(out.toByteArray());
}

} 

用法 :

String serviceUri = "service url";
    InputStream fileInputStream = mInputStream; //Your file stream
    String fileName = "your file name";
    String fileKey = "Key name what server is looking for"
    HashMap<String, String> headerparts = mHeaderParts; //Other header parts that you need to send along.

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(serviceUri);
    SimpleMultipartEntity entity = new SimpleMultipartEntity();
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-type", "multipart/form-data; boundary="
            + entity.getBoundary());
    entity.writeFirstBoundaryIfNeeds();

    if (headerparts != null) {
        Object[] keySet = headerparts.keySet().toArray();
        for (int i = 0; i < keySet.length; i++) {
            String key = keySet[i].toString();
            String value = headerparts.get(key);
            entity.addPart(key, value);
        }
    }

    entity.addPart(fileKey, fileName, fileInputStream);
    entity.writeLastBoundaryIfNeeds();
    httpPost.setEntity(entity);

    try {
        mResponse = httpClient.execute(httpPost);
    } catch (Exception e) {
        e.printStackTrace();
    }
于 2012-10-03T04:52:18.950 回答