Sue Zheng 2016-05-08 07:42 采纳率: 43.8%
浏览 18392
已结题

上传图片到服务器 because no multipart boundary was found

各路神仙看过来啊……
问题:服务端输出:the request was rejected because no multipart boundary was found
客户端代码:
new AsyncTask() {
protected void onPostExecute(String result) {
etNotice.append("success"+result);
};
@Override
protected String doInBackground(Void... arg0) {
String r=null;
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(Config.SERVER_URL+Config.ACTION_Noticeinfo);
FileEntity reqEntity = new FileEntity(file, "binary/octet-stream");
reqEntity.setContentType("multipart/form-data");

httppost.setEntity(reqEntity);
try {
//httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
r=String.valueOf(response.getStatusLine().getStatusCode());

if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return r;
}
}.execute();

服务器代码:
private void uploadImg(HttpServletRequest request,
HttpServletResponse response) throws IOException {
DiskFileItemFactory factory = new DiskFileItemFactory();

ServletFileUpload upload = new ServletFileUpload(factory);

upload.setFileSizeMax(1024*1024); //设置上传文件的最大容量

try{

List items = (List)upload.parseRequest(new ServletRequestContext(request));
// Listitems = upload.parseRequest( (RequestContext) request); //取得表单全部数据

for(FileItem item:items){

if(!item.isFormField()){ //如果是上传的文件

String name = "D:\"+item.getName().substring(item.getName().lastIndexOf('\')+1);

String filename = name;

System.out.println(filename);

File f = new File(filename); //保存到D盘

item.write(f);

System.out.println("上传成功");

}

}

}

catch(Exception e){

e.printStackTrace();

}
}
客户端收到的status为200说明数据传到了服务器,但是服务器出现了错误:the request was rejected because no multipart boundary was found,请问怎么办啊?

  • 写回答

1条回答

  • Robot-S 2016-05-08 07:52
    关注

    出现此错误:
    $ gitk --all
    Wish[33464:707] CFURLCopyResourcePropertyForKey failed because it was passed this URL which has no scheme:
    Wish[33464:707] CFURLCopyResourcePropertyForKey failed because it was......
    答案就在这里:CFURLCopyResourcePropertyForKey failed because it was passed this URL which has no scheme:
    ----------------------Hi,地球人,我是问答机器人小S,上面的内容就是我狂拽酷炫叼炸天的答案,除了赞同,你还有别的选择吗?

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮