使用params将文件上传到服务器

时间:2014-02-25 07:30:47

标签: android

我正在尝试使用其他参数上传文件到服务器,但没有成功:

来自网络控制台表单的数据:

Request URL: ../upload-photo
Request Method:POST
Status Code:403 Forbidden
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Connection:keep-alive
Content-Length:753249
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryKWTNLV05FNLs29bB
X-Requested-With:XMLHttpRequest
Request Payload
------WebKitFormBoundaryKWTNLV05FNLs29bB
Content-Disposition: form-data; name="latitude"

1212.22
------WebKitFormBoundaryKWTNLV05FNLs29bB
Content-Disposition: form-data; name="longitude"

1212.22
------WebKitFormBoundaryKWTNLV05FNLs29bB
Content-Disposition: form-data; name="start_record"

212
------WebKitFormBoundaryKWTNLV05FNLs29bB
Content-Disposition: form-data; name="file"; filename="IMAG2286.jpg"
Content-Type: image/jpeg

我的代码:

public static void uploadFile(String filePath) {

        FileInputStream fileInputStream = null;
        HttpURLConnection connection = null;
        DataOutputStream dataOutputStream = null;
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;
        File sourceFile = new File(filePath);

        if (!sourceFile.isFile()) {
            Log.e("uploadFile", "Source File not exist ");
        } else {

            try {

                fileInputStream = new FileInputStream(sourceFile);
                URL url = new URL(SERVER_URI);
                connection = (HttpURLConnection) url.openConnection();

                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.setUseCaches(false);
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Connection", "Keep-Alive");
                connection.setRequestProperty("Content-Type",
                        "multipart/form-data; boundary=----WebKitFormBoundaryKWTNLV05FNLs29bB");

                dataOutputStream = new DataOutputStream(
                        connection.getOutputStream());


                dataOutputStream.writeBytes(BOUNDARY);
                dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"latitude\"\n");
                dataOutputStream.writeBytes("44.44");
                dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"longitude\"\n");
                dataOutputStream.writeBytes("123");
                dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"start_record\"\n");
                dataOutputStream.writeBytes("123");
                dataOutputStream.writeBytes("------WebKitFormBoundaryKWTNLV05FNLs29bB\n" +
                        "Content-Disposition: form-data; name=\"file\"; filename=\"IMAG2286.jpg\"\n" +
                        "Content-Type: image/jpeg\n");


                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                while (bytesRead > 0) {

                    dataOutputStream.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                }

                dataOutputStream.writeBytes(lineEnd);
                dataOutputStream.writeBytes(separator + boundary + separator
                        + lineEnd);


            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fileInputStream.close();
                    dataOutputStream.flush();
                    dataOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }
    }

2 个答案:

答案 0 :(得分:0)

您可以使用准备好的MultiPartEntity来执行此操作:

HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpClient = new DefaultHttpClient(params);

try {
    HttpPost httpPost = new HttpPost(Statics.BASE_URL + "/");

    MultipartEntity multipartEntity = new MultipartEntity();

    if (imagePath.length() > 0) {
        multipartEntity.addPart("images", new FileBody(new File(imagePath)));
    }

    httpPost.setEntity(multipartEntity);

    String resp = httpClient.execute(httpPost, new BasicResponseHandler());

} catch (Exception e) {
}

答案 1 :(得分:0)

你可以使用async-http-client库。

使用起来太容易了

here