HttpClient上传、接收图片
来源:互联网 发布:图像在线批处理软件 编辑:程序博客网 时间:2024/06/13 02:30
转载于:http://jingtaohailang.blog.51cto.com/8862159/1544821
感谢博主: 惊涛骇浪
正文:
1、客户端:
用到的jar包有:
commons-codec-1.3.jar
commons-httpclient.jar
commons-logging1.1.jar
代码:
package com.httpclient;
import java.io.File;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
public class Hclient {
public static void main(String args[]) {
String targetURL = null; //– 指定URL
File targetFile = null; //– 指定上传文件
targetFile = new File(“E:\1.jpg”);
targetURL = “http://localhost:7081/sendmessa/testServlet“; // servleturl
PostMethod filePost = new PostMethod(targetURL);
try {
//通过以下方法可以模拟页面参数提交
//filePost.setParameter(“name”, “中文”);
//filePost.setParameter(“pass”, “1234”);
Part[] parts = { new FilePart(targetFile.getName(), targetFile) };
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
int status = client.executeMethod(filePost);
if (status == HttpStatus.SC_OK) {
System.out.println(“上传成功”);// 上传成功
} else {
System.out.println(“上传失败”);// 上传失败
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
filePost.releaseConnection();
}
}
}
2、服务器端:
新建Web Project,工程名:netWorkServer。在src文件夹下创建com.httpclient的Package,在该包下创建名为TestServlets的Servlet。
用到的jar包是:commons-fileupload.jar。代码如下:
代码:
package com.httpclient;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class TestServlets extends HttpServlet {
private static final long serialVersionUID = 1L;
private String uploadPath = “d:\temp”; // 上传文件的目录
private String tempPath = “d:\temp\buffer\”; //临时文件目录
File tempPathFile;
public void init() throws ServletException {
File uploadFile = new File(uploadPath);
if (!uploadFile.exists()) {
uploadFile.mkdirs();
}
File tempPathFile = new File(tempPath); if (!tempPathFile.exists()) { tempPathFile.mkdirs(); }}@SuppressWarnings("unchecked")public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { try { // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); // Set factory constraints factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb factory.setRepository(tempPathFile);// 设置缓冲区目录 // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Set overall request size constraint upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB List<FileItem> items = upload.parseRequest(request);// 得到所有的文件 Iterator<FileItem> i = items.iterator(); while (i.hasNext()) { FileItem fi = (FileItem) i.next(); String fileName = fi.getName(); if (fileName != null) { File fullFile = new File(fi.getName()); File savedFile = new File(uploadPath, fullFile.getName()); fi.write(savedFile); } } System.out.print("upload succeed"); } catch (Exception e) { System.out.println(e.getMessage()); // 可以跳转出错页面 e.printStackTrace(); }}
}
- HttpClient上传、接收图片
- android HttpClient 上传图片
- android HttpClient 上传图片
- httpclient 上传图片
- 用HttpClient上传图片
- FCK上传图片,接收图片
- java httpclient 上传文件 服务器 servlet 接收
- HttpClient上传图片,下载文件
- Ios上传图片 后台接收
- Android 上传图片,服务器接收图片实现
- okhttp方法上传图片和接收图片
- 控制器接收图片和上传图片写法
- 使用httpclient模拟表单上传文件,后台用struts2接收
- 使用httpclient模拟表单提交,上传图片
- httpClient上传图片文件的功能
- CKEditor之servlet接收图片上传
- 苹果客户端上传图片java接收
- php接收ios/android上传图片
- hdu4542 小明系列故事——未知剩余系(反素数打表待改!!不会!!)
- 突然间的一个阿里电话面试
- 读《重构》后在android里的一些应用
- SQL数据库中的范式
- Combination Sum
- HttpClient上传、接收图片
- jquery 思维导图
- 1122. Hamiltonian Cycle (25)
- 杭电ACM 2001.计算两点间的距离
- 112. Path Sum
- 《Model Predictive Control of Swarms of Spacecraft Using Sequential Convex Programming》
- C++第1次实验-项目2
- JavaSE 学习参考:方法的基本概念
- 移动互联网APP测试点