android上传文件
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;
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);
}
<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
{
}
}
相关推荐
首先,让我们理解`fileList = upload.parseRequest(request);`这行代码在文件上传过程中的作用。`upload.parseRequest(request)`是`Commons FileUpload`库中的一个方法,它解析HTTP请求,从中提取出上传的文件信息。...
List<FileItem> items = upload.parseRequest(request); Iterator<FileItem> iter = items.iterator(); while (iter.hasNext()) { FileItem item = iter.next(); if (item.isFormField()) { // 处理普通表单...
然后,创建一个`ServletFileUpload`实例,并使用`parseRequest()`方法解析请求。这个方法会返回一个`FileItemIterator`,你可以遍历它来获取所有表单字段,包括文件和文本字段。 ```java import org.apache....
try {List items = upload.parseRequest(request);Iterator itr = items.iterator(); while (itr.hasNext()) {FileItem item = (FileItem) itr.next();if (item.isFormField()) { System.out.println("表单参数名:" ...
items = upload.parseRequest(request); for (FileItem item : items) { if (!item.isFormField()) { String fileName = item.getName(); File uploadedFile = new File("uploads/" + fileName); item.write...
List fileItems = upload.parseRequest(request); //开始读取上传信息 Iterator iter = fileItems.iterator(); // 依次处理每个上传的文件 while(iter.hasNext()) { FileItem item = (FileItem) iter.next(); // ...
List<FileItem> items = upload.parseRequest(request); for (FileItem item : items) { if (!item.isFormField()) { String fileName = item.getName(); File storeFile = new File("/path/to/save/" + ...
httpUpload.parseRequest(); } catch (IOException e) { // 处理异常 } // 获取上传文件列表 List<HttpUpload.FileItem> fileItems = httpUpload.getFileItems(); } ``` 3. **处理上传文件**:遍历`...
List<FileItem> items = upload.parseRequest(request); for (FileItem item : items) { if (!item.isFormField()) { String fileName = item.getName(); // 保存文件到服务器 item.write(new File(...
Request request = new Request.Builder() .url(url) .post(requestBody) .build(); ``` 4. **发送请求**:使用 OkHttpClient 发送请求并处理响应。这里需要注意,由于我们没有后台接口支持,所以需要模拟一个...
List<FileItem> list = upload.parseRequest(request); for (FileItem item : list) { if (item.isFormField()) { String name = item.getFieldName(); String value = item.getString("GBK")...
- 上述代码首先通过`request.getParameter("method")`获取请求中的`method`参数,判断是否为"upload",如果是,则执行文件上传逻辑。 ### 2. 文件名处理与保存路径设置 #### 核心概念 - **文件名处理**: 为了避免...
上传文件+ajax进度条 FileItemFactory factory = new DiskFileItemFactory(); // 通过该工厂对象创建...List items = upload.parseRequest(request); for (Iterator i = items.iterator(); i.hasNext();) {
// 创建文件上传解析器List<FileItem> items = upload.parseRequest(req); // 解析请求中的表单数据for (FileItem item : items) { if (!item.isFormField()) { // 如果不是普通表单字段,那么它就是一个文件字段...
List fileItems = upload.parseRequest(request); Iterator iter = fileItems.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (!item.isFormField()) { ...
MultiPartRequest request = new MultiPartRequest(Request.Method.POST, UPLOAD_URL, YourResponseClass.class, new Response.Listener() {...}, new Response.ErrorListener() {...}); request.addFilePart(...
List<FileItem> items = upload.parseRequest(request); for (FileItem item : items) { if (!item.isFormField()) { String fileName = item.getName(); File targetFile = new File(uploadDirectory, fileName)...
List<FileItem> list = upload.parseRequest(request); 每个FileItem对象对应一个表单项,包含了表单项的详细信息,例如字段名、文件名、文件内容、文件类型等。FileItem对象提供了多种方法,例如: * ...
List<FileItem> items = upload.parseRequest(request); ``` ##### 示例3:控制文件大小 ```java // 创建基于磁盘的文件项工厂,并设置最大内存大小和临时目录 DiskFileItemFactory factory = new ...
items=upload.parseRequest (request).iterator(); while(items.hasNext()){ FileItem item=(FileItem) items.next(); if(!item.isFormField()){ String name=item.getName (); String fileName=name....