各路神仙看过来啊……
问题:服务端输出: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,请问怎么办啊?