`

【转】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/
分享到:
评论

相关推荐

    common-fileupload实例:fileList = upload.parseRequest(request);上传file文件和form表单提交

    首先,让我们理解`fileList = upload.parseRequest(request);`这行代码在文件上传过程中的作用。`upload.parseRequest(request)`是`Commons FileUpload`库中的一个方法,它解析HTTP请求,从中提取出上传的文件信息。...

    利用org.apache.commons.fileupload上传多个文件

    List&lt;FileItem&gt; items = upload.parseRequest(request); Iterator&lt;FileItem&gt; iter = items.iterator(); while (iter.hasNext()) { FileItem item = iter.next(); if (item.isFormField()) { // 处理普通表单...

    java 页面上有ENCTYPE="multipart/form-data" 时action方法或者控制层用request.getParameter()获取不到值的方法 ?

    然后,创建一个`ServletFileUpload`实例,并使用`parseRequest()`方法解析请求。这个方法会返回一个`FileItemIterator`,你可以遍历它来获取所有表单字段,包括文件和文本字段。 ```java import org.apache....

    JSP-文件图片上传-fileupload组件-commons-upload

    try {List items = upload.parseRequest(request);Iterator itr = items.iterator(); while (itr.hasNext()) {FileItem item = (FileItem) itr.next();if (item.isFormField()) { System.out.println("表单参数名:" ...

    jsp上传文件实例可以使用jsp上传图片,文档,等文件

    items = upload.parseRequest(request); for (FileItem item : items) { if (!item.isFormField()) { String fileName = item.getName(); File uploadedFile = new File("uploads/" + fileName); item.write...

    jspsmartupload

    List fileItems = upload.parseRequest(request); //开始读取上传信息 Iterator iter = fileItems.iterator(); // 依次处理每个上传的文件 while(iter.hasNext()) { FileItem item = (FileItem) iter.next(); // ...

    servletUpload

    List&lt;FileItem&gt; items = upload.parseRequest(request); for (FileItem item : items) { if (!item.isFormField()) { String fileName = item.getName(); File storeFile = new File("/path/to/save/" + ...

    java上传文件(upload)

    httpUpload.parseRequest(); } catch (IOException e) { // 处理异常 } // 获取上传文件列表 List&lt;HttpUpload.FileItem&gt; fileItems = httpUpload.getFileItems(); } ``` 3. **处理上传文件**:遍历`...

    jspupload文件上传

    List&lt;FileItem&gt; items = upload.parseRequest(request); for (FileItem item : items) { if (!item.isFormField()) { String fileName = item.getName(); // 保存文件到服务器 item.write(new File(...

    uploadpicture

    Request request = new Request.Builder() .url(url) .post(requestBody) .build(); ``` 4. **发送请求**:使用 OkHttpClient 发送请求并处理响应。这里需要注意,由于我们没有后台接口支持,所以需要模拟一个...

    ognl源码包值得用一用

    List&lt;FileItem&gt; list = upload.parseRequest(request); for (FileItem item : list) { if (item.isFormField()) { String name = item.getFieldName(); String value = item.getString("GBK")...

    upload,upload

    - 上述代码首先通过`request.getParameter("method")`获取请求中的`method`参数,判断是否为"upload",如果是,则执行文件上传逻辑。 ### 2. 文件名处理与保存路径设置 #### 核心概念 - **文件名处理**: 为了避免...

    上传文件+ajax进度条

    上传文件+ajax进度条 FileItemFactory factory = new DiskFileItemFactory(); // 通过该工厂对象创建...List items = upload.parseRequest(request); for (Iterator i = items.iterator(); i.hasNext();) {

    jspsmart文件上传与邮件发送的实例.docx

    // 创建文件上传解析器List&lt;FileItem&gt; items = upload.parseRequest(req); // 解析请求中的表单数据for (FileItem item : items) { if (!item.isFormField()) { // 如果不是普通表单字段,那么它就是一个文件字段...

    flex中的文件上传(简单例子)

    List fileItems = upload.parseRequest(request); Iterator iter = fileItems.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (!item.isFormField()) { ...

    Volley#Request(GsonRequest,MultiPartRequest)

    MultiPartRequest request = new MultiPartRequest(Request.Method.POST, UPLOAD_URL, YourResponseClass.class, new Response.Listener() {...}, new Response.ErrorListener() {...}); request.addFilePart(...

    JSP_fileupload.rar_fileupload jsp_jsp file upload_jsp fileupload

    List&lt;FileItem&gt; items = upload.parseRequest(request); for (FileItem item : items) { if (!item.isFormField()) { String fileName = item.getName(); File targetFile = new File(uploadDirectory, fileName)...

    使用fileupload组件实现文件上传功能

    List&lt;FileItem&gt; list = upload.parseRequest(request); 每个FileItem对象对应一个表单项,包含了表单项的详细信息,例如字段名、文件名、文件内容、文件类型等。FileItem对象提供了多种方法,例如: * ...

    上传组件Commons fileUpload.jar剖析

    List&lt;FileItem&gt; items = upload.parseRequest(request); ``` ##### 示例3:控制文件大小 ```java // 创建基于磁盘的文件项工厂,并设置最大内存大小和临时目录 DiskFileItemFactory factory = new ...

    java通过js上传文件

    items=upload.parseRequest (request).iterator(); while(items.hasNext()){ FileItem item=(FileItem) items.next(); if(!item.isFormField()){ String name=item.getName (); String fileName=name....

Global site tag (gtag.js) - Google Analytics