2 guomin beiluoshimen guomin_beiluoshimen 于 2016.05.08 15:42 提问

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

各路神仙看过来啊……
问题:服务端输出: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个回答

CSDNXIAOS
CSDNXIAOS   2016.05.08 15: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,上面的内容就是我狂拽酷炫叼炸天的答案,除了赞同,你还有别的选择吗?

guomin_beiluoshimen
guomin_beiluoshimen 阴魂不散
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!