图片上传时出现Android 500错误

时间:2017-01-19 13:45:43

标签: java android multipartform-data

我使用multipart获得500服务器错误。从iOS端上传图像时,相同的服务器代码正在运行。以下是我的多部分图像上传代码.Server Side正在使用PHP。

public PostFile(String requestURL)
throws IOException {
    // creates a unique boundary based on time stamp
    URL url = new URL(requestURL);
    httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setUseCaches(false);
    httpConn.setDoOutput(true); // indicates POST method
    httpConn.setDoInput(true);
    httpConn.setRequestMethod("POST");
    httpConn.setRequestProperty("Connection", "Keep-Alive");
    httpConn.setRequestProperty("Cache-Control", "no-cache");
    httpConn.setRequestProperty(
        "Content-Type", "multipart/form-data;boundary=" + this.boundary);

    request = new DataOutputStream(httpConn.getOutputStream());
}
private void saveFile() {
    try {
        PostFile postFile = new PostFile(URL);
        //postFile.addFormField();
        postFile.addFormField("userId", "25");
        postFile.addFormField("accessToken", "h7lCesyM3XKjmjvjrdojzypUNPqA9MsB8PQIzZyWkYHtV43XcxPubUJ3EV8L");
        // contact.accumulate("files",imgData);
        postFile.addFormField("area", "dfs");
        postFile.addFilePart("test.jpeg", getFileFromBitmap(uri));
        postFile.finish();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public void addFilePart(String fieldName, File uploadFile)
throws IOException {
    String fileName = uploadFile.getName();
    request.writeBytes(this.twoHyphens + this.boundary + this.crlf);
    request.writeBytes("Content-Disposition: form-data; name=\"" +
        fieldName + "\";filename=\"" +
        fileName + "\"" + this.crlf);
    request.writeBytes("Content-Type:  image/jpeg;" + this.crlf);
    request.writeBytes(this.crlf);

    // byte[] bytes = Files.readAllBytes(uploadFile.toPath());

    request.write(getBytesFromFile(uploadFile));
}

0 个答案:

没有答案
相关问题