HttpClient远程发送文件
来源:互联网 发布:乎油穿越火线卡枪工具 编辑:程序博客网 时间:2024/05/13 04:22
应用程序部分:
注意:要载入commons-httpclient-3.1.jar commons-codec.jar commons-logging.jar这三个包
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;// TODO 指定URL
File targetFile = null;// TODO 指定上传文件
targetFile = new File("1.mp3");
targetURL = "http://localhost:8080/test/tt"; //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();
}
}
}
servlet部分:
注意:需求载入commons-fileupload-1.2.1.jar commons-io.jar这两个包
package servlet;
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 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();
}
}
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();
}
}
}
在web.xml中添加映射,就可以实现应用程序上传文件到servlet的功能了。
- HttpClient远程发送文件
- HttpClient实现远程发送servlet通信
- HttpClient发送文件与报文
- httpclient 发送文件和字符串信息
- HttpClient 向tomcat服务器发送文件
- httpClient发送key_value、json参数及文件
- HttpClient发送http请求以及上传文件
- 使用HttpClient发送InputStream对象及发送文件
- java 远程 发送文件到服务器
- 使用HttpClient携带文件发送请求简单实例
- HttpClient:模拟表单提交文件,发送multipart/form-data数据
- httpClient技术发送带有文件,文本内容的信息
- Struts2 结合HttpClient 实现远程服务器文件下载
- java上传文件到远程服务器(二)---HttpClient方式
- http请求远程接口获得Json数据(2) --- 利用HttpClient发送post/get请求
- 使用HttpClient发送HttpPost请求包含上传本地图片和远程图片的传输实现
- HttpClient发送URL
- Httpclient发送POST请求
- SEO如何优化网页标题
- 用Java实现将本机表数据转到Oracle表中
- Google左侧排名
- 东软NetEye中小型企业安全解决方案
- 批处理备份
- HttpClient远程发送文件
- 您的文章已经被推荐到CSDN首页业界评点栏目——这是怎么回事?
- 针对新网站要做的几个SEO
- Android能用Linux打败Linux手机吗?
- 逐浪CMS软件老总的年度工作报告泄密
- 2009.01.06版发布。
- JS正确截取单字节和双字节混和字符串的方法
- 单条信息向上滚动
- jdbc+proxool