1
HttpClient httpclient1 = new DefaultHttpClient();

HttpParams p=new BasicHttpParams();

p.setParameter("vname",name);
p.setParameter("address", addr);
p.setParameter("age", age);
p.setParameter("contact", cnct);
p.setParameter("gender", gen);
p.setParameter("whomto", wtm);
p.setParameter("myFile", f);

HttpPost res1=new HttpPost(result);
res1.setHeader("Content-Type", "text/plain");
res1.setHeader("Content-Type","image/jpeg");

HttpResponse response1 = httpclient1.execute(res1);
HttpEntity entity1 = response1.getEntity();
i1 = entity1.getContent();
BufferedReader reader1 = new BufferedReader(new InputStreamReader(i1,"iso-8859-1"),8);
StringBuilder sb1 = new StringBuilder();
String line1 = null;

if((line1 = reader1.readLine()) != null) {

    sb1.append(line1);
    back=sb1.toString();
}
else{
    Log.e("GET data","null");
}
i1.close();
Log.e("GET",""+back);  

//服务器代码 private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {

    res.setContentType("text/html");

    Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
    BlobKey blobKey = blobs.get("myFile");
    final BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(new BlobKey(blobKey.getKeyString()));
    System.out.println(blobInfo.getContentType());
    if(!blobInfo.getContentType().equalsIgnoreCase("image/jpeg")){
        blobstoreService.delete(blobInfo.getBlobKey());
        res.getWriter().println("Please Provide JPG image only");

    }       

我正在向服务器发送一个带有其他数据的图像文件。我没有收到任何错误或异常但是当我打印“返回”时,在日志中显示“此 URL 不接受请求的内容类型”。“f”是我的图像文件..有什么问题?

4

2 回答 2

0

看看我对与您类似的问题的回答:链接 它将为您省去编写服务器端代码并​​让您的 android 应用程序发送数据的麻烦,例如。图像给它。

于 2012-08-09T07:41:34.740 回答
0

您正在将 Content-Type 标头设置为两个不同的值,这两个值都不是“multipart/form-data”,我相信这是您在发送文件时需要的。

这是一个应该有帮助的答案: https ://stackoverflow.com/a/3003402/412558

于 2013-09-20T20:20:08.193 回答