【转】upload.parseRequest(request)为空解决办法

android上传文件

httpclient 上传文件 后台struts使用action
 
action

HttpServletRequest request =ServletActionContext.getRequest();

DiskFileItemFactory factory =newDiskFileItemFactory();
factory.setSizeThreshold(4096);
System.out.println("保存路径:"+ getSavePath());
ServletFileUpload upload =newServletFileUpload(factory);
upload.setFileSizeMax(419430400);

List<?> items = upload.parseRequest(request);
Iterator<?> i = items.iterator();
while(i.hasNext()){
FileItemfi=(FileItem) i.next();
String filename =fi.getName();

if(filename !=null){
File file =newFile(filename);
File savefile =newFile(getSavePath(), file.getName());
fi.write(savefile);
System.out.println("保存路径:"+ getSavePath());
}
}

return SUCCESS;

 
htttpclient

FileBody bin =newFileBody(newFile("preview-ipad-01.jpg"));
HttpClient client =newDefaultHttpClient();
HttpPost post =newHttpPost("http://192.2.2.73:8080/upload/upload/uphc.do");
StringBody username =newStringBody("ssss");
StringBody password =newStringBody("dddd");

MultipartEntity reqEntity =newMultipartEntity();
reqEntity.addPart("username", username);
reqEntity.addPart("password", password);

reqEntity.addPart("data", bin);
post.setEntity(reqEntity);
System.out.println(post.getRequestLine()+"============");;

HttpResponse response = client.execute(post);
System.out.println("返回标志:"+ response.getStatusLine().getStatusCode());

HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
if(resEntity !=null){
System.out.println("长度:"+ resEntity.getContentLength());
System.out.println("类型:"+ resEntity.getContentType());
System.out.println("编码:"+ resEntity.getContentEncoding());

InputStreamin= resEntity.getContent();
int i =in.available();
byte[] b =newbyte[i];
in.read(b);
in.close();

String xxx =newString(b);
System.out.println(xxx);


}

struts中要配置一下  

<beantype="org.apache.struts2.dispatcher.multipart.MultiPartRequest"
name="myRequestParser"class="com.mypkg.RequestParseWrapper"
scope="default"optional="true"/>
<constantname="struts.multipart.handler"value="myRequestParser"/>

这个必须有还要建立个空的类

publicclassRequestParseWrapperextendsJakartaMultiPartRequest{

publicvoid parse(HttpServletRequest servletRequest,String saveDir)throwsIOException
{

}
}

然后才能行
转自:http://school512.blog.163.com/blog/static/231691942013320113941216/

猜你喜欢

转载自gdfdfg-tech.iteye.com/blog/2105632
今日推荐