为什么这个POST请求通过HttpURLConnection抛出FileNotFoundException?

时间:2015-07-23 14:01:32

标签: java android error-handling upload httpurlconnection

更新3:我尝试对Blobstore URL的insteaad请求执行此请求并发生相同的异常,因此这看起来像我的上传方法使用HttpURLConnection而不是特定于Blobstore的问题。

更新2:添加了用于在底部创建上传网址方法的代码

更新1:在问题下方添加了代码和确切异常。希望现在的情况更加清晰。

使用对createUploadURL()方法提供的URL的POST请求,文件将上传到Blobstore。但是,App Engine仍会对上传请求响应FileNotFoundException,抱怨在https://<app-id>.appspot.com/_ah/upload/<long-random-string>找不到文件

这可能会发生什么?

上传代码:

try {
    MultipartUtility multipartUtility = new MultipartUtility(blobUploadURL, "UTF-8",writeListener);
    multipartUtility.addFormField("key_name", keyName);
    multipartUtility.addFilePart("file", fileName, is);
    multipartUtility.finish();

} catch (IOException e) {
}

MultipartUtility Class:

public class MultipartUtility {
    private final String boundary;
    private static final String LINE_FEED = "\r\n";
    private HttpURLConnection httpConn;
    private String charset;
    private OutputStream outputStream;
    private PrintWriter writer;
    private WriteListener listener;

    public static interface WriteListener{

        void transferred(long num);

    }

    public MultipartUtility(String requestURL, String charset,WriteListener listener)
            throws IOException {
        this.charset = charset;

        // creates a unique boundary based on time stamp
        boundary = "===" + System.currentTimeMillis() + "===";

        URL url = new URL(requestURL);
        httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setUseCaches(false);
        httpConn.setDoOutput(true); // indicates POST method
        httpConn.setDoInput(true);
        httpConn.setRequestProperty("Content-Type",
                "multipart/form-data; boundary=" + boundary);
        httpConn.setChunkedStreamingMode(-1);
        outputStream=httpConn.getOutputStream();
        this.listener=listener;
        writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
                true);
    }


    public void addFormField(String name, String value) {
        writer.append("--" + boundary).append(LINE_FEED);
        writer.append("Content-Disposition: form-data; name=\"" + name + "\"")
                .append(LINE_FEED);
        writer.append("Content-Type: text/plain; charset=" + charset).append(
                LINE_FEED);
        writer.append(LINE_FEED);
        writer.append(value).append(LINE_FEED);
        writer.flush();
    }


    public void addFilePart(String fieldName, String fileName, InputStream is)
            throws IOException {
        //String fileName = uploadFile.getName();
        writer.append("--" + boundary).append(LINE_FEED);
        writer.append(
                "Content-Disposition: form-data; name=\"" + fieldName
                        + "\"; filename=\"" + fileName + "\"")
                .append(LINE_FEED);
        writer.append(
                "Content-Type: "
                        + URLConnection.guessContentTypeFromName(fileName))
                .append(LINE_FEED);
        writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
        writer.append(LINE_FEED);
        writer.flush();

        byte[] buffer = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = is.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        outputStream.flush();

        writer.append(LINE_FEED);
        writer.flush();
    }

    public void addHeaderField(String name, String value) {
        writer.append(name + ": " + value).append(LINE_FEED);
        writer.flush();
    }

    public List<String> finish() throws IOException {
        List<String> response = new ArrayList<String>();

        writer.append(LINE_FEED).flush();
        writer.append("--" + boundary + "--").append(LINE_FEED);
        writer.close();

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpConn.getInputStream()));
        String line = null;
        while ((line = reader.readLine()) != null) {
            response.add(line);
        }

        // checks server's status code first
        int status = httpConn.getResponseCode();
        if (status != HttpURLConnection.HTTP_OK) {
            throw new IOException("Server returned non-OK status: " + status);
        }


        reader.close();
        httpConn.disconnect();

        return response;
    }

}

执行上传请求时捕获到IO异常:

java.io.FileNotFoundException: https://<app-id>.appspot.com/_ah/upload/<random-long-string>

创建Blobstore上传URL的代码:(请注意,我可以在Blobstore中看到上传的文件,甚至确认blob处理程序servlet也获取了上传的blob的密钥,尽管存在此异常。)

BlobstoreService bs = BlobstoreServiceFactory.getBlobstoreService();
String blobUploadURL = bs.createUploadUrl("/save/blob");

0 个答案:

没有答案