使用应用引擎时,500内部服务器错误,尝试上传图像

时间:2013-12-30 04:41:53

标签: java google-app-engine http http-post multipart

我正在尝试将图像从Android上传到应用引擎(Java)。我使用BlobstoreService来存储图像。这是我的项目的样子:

在服务器端,我执行以下操作来生成上传URL

BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
String uploadURL = blobstoreService.createUploadUrl("/upload");
resp.getWriter().print(uploadURL);

在客户端(Android)方面,我在打印时得到了这个

http://mydomain.com/_ah/upload/AMmfu6YehP-o-VUwRXmCfJzYOZ1jvXiD33fF-LhZw5nO6XIwCE0cD0-zTwvg9beO0gXeNjuNG_wMNwnX9Yr5I3BVUdyolby4bnXQyHBWA-fpJNfR7sfqqukOIuMEa3EubgNWTj7q0bxY/ALBNUaYAAAAAUsDnWeK48TX7bblzmE0jUNgf22o4-IXP/

请注意网址中的最后一个字符'/'。当我使用它作为url构建httpurl'HttpPost(mUploadURL)'时,我在url的索引231(即'/')的路径中得到非法字符。所以我的问题是 - 我在这里做错了什么?

所以,我从url中删除了'/',如下所示:

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = null;
        postRequest = new HttpPost(mUploadURL.substring(0, mUploadURL.length()-2));
        try {
        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        reqEntity.addPart("title", new StringBody("new title"));

        try{
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Log.d("test", "mPhoto: " + mPhoto);
            mPhoto.compress(CompressFormat.JPEG, 75, bos);
            byte[] data = bos.toByteArray();
            ByteArrayBody bab = new ByteArrayBody(data, "image/jpeg", "forest.jpg");
            reqEntity.addPart("picture", bab);
        }
        catch(Exception e){
            Log.d("test", "e: " + e);
            reqEntity.addPart("picture", new StringBody(""));
        }
        postRequest.setEntity(reqEntity);
        HttpResponse response = httpClient.execute(postRequest);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String sResponse;
        StringBuilder s = new StringBuilder();
        while ((sResponse = reader.readLine()) != null) {
            s = s.append(sResponse);
        }
        Log.d("test", "after post: " + s.toString());
        return s.toString();
        } catch (Exception ee) {
            Log.d("test", "ee: " + ee);
            return "";
        }

发帖后,我得到了结果

<html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"><title>500 Server Error</title></head><body text=#000000 bgcolor=#ffffff><h1>Error: Server Error</h1><h2>The server encountered an error and could not complete your request.<p>Please retry your last submission.</p><p>If the problem persists, please contact the person responsible for the application you're using, or, if you are that person, <a href="http://code.google.com/appengine/community.html">report</a> your problem and mention this error message and the query that caused it.</h2></body></html>

我的第二个问题是 - 多部分邮政编码有什么问题?我使用app引擎作为后端。任何帮助将不胜感激!

0 个答案:

没有答案