package com.demo.http;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class HttpPostServlet extends HttpServlet {
private String userid;
private String password;
private String msg;
private FileItem myfile;
private String bufferpath;//上传文件时的缓冲区位置
private String savepath;//上传上来的文件保存位置
/**
*
*/
private static final long serialVersionUID = -2122613601586478063L;
/**
* Constructor of the object.
*/
public HttpPostServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=gbk");
bufferpath = "D:\\temp\\buffer\\";//上传文件时的缓冲区位置
savepath = "D:\\temp\\smstemps\\";//上传上来的临时文件保存位置
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
File savedir = new File(savepath);//设置文件保存目录
if (!savedir.isDirectory()) {
savedir.mkdirs();
}
File bufferdir = new File(bufferpath);
if (!bufferdir.isDirectory()) {
bufferdir.mkdirs();
}
factory.setRepository(bufferdir);// 设置缓冲区目录 因为是上传文件的表单类型 所以 表单里面的所有属性都是 以字节流的形式穿到后台
//并临时存放到缓冲区目录
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(41943040); // 设置最大文件尺寸,这里是40MB
List<FileItem> items;
try {
items = upload.parseRequest(request);
Iterator<FileItem> i = items.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
// fi.isFormField() 如果是true 则是普通属性 如果是false 则是文件
if (fi.isFormField()) {
if ("userid".equals(fi.getFieldName())) {
userid = fi.getString();
}
if ("password".equals(fi.getFieldName())) {
password = fi.getString();
}
if ("msg".equals(fi.getFieldName())) {
msg = fi.getString();
}
}
if ("myfile".equals(fi.getFieldName())) {//对上传过来的 文件单独进行处理
myfile = fi;
try {
myfile.write(new File(savedir, fi.getName()));//将文件保存起来
//myfile.write(new File("D://asdf/adsf/adsf.txt"));//也可以这样写 直接指定自己的文件路径和文件名字
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (FileUploadException e1) {
e1.printStackTrace();
}
PrintWriter out = response.getWriter();
out.println("表单里面的数据 " + userid + password + msg);
out.close();
out = null;
}
/**
* Initialization of the servlet. <br>
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
HttpClient PostMethod 上传文件
共19个文件
jar:7个
java:3个
class:3个
5星 · 超过95%的资源 需积分: 49 148 下载量 127 浏览量
2011-12-28
15:40:01
上传
评论 1
收藏 2.83MB ZIP 举报
温馨提示
本人自己总结的httpClient PostMethod 上传文件完整实例 以及 使用java PostMethod 和GetMethod 发送请求实例 及乱码处理
资源推荐
资源详情
资源评论
收起资源包目录
Demo.zip (19个子文件)
Demo
.project 1006B
.mymetadata 285B
src
com
demo
http
FilePostOne.java 2KB
HttpClientTest.java 2KB
HttpPostServlet.java 4KB
WebRoot
WEB-INF
classes
com
demo
http
FilePostOne.class 3KB
HttpPostServlet.class 4KB
HttpClientTest.class 3KB
lib
log4j.jar 347KB
commons-io-1.2.jar 64KB
commons-httpclient-3.1.jar 298KB
commons-codec-1.3.jar 46KB
axis2-1.2.jar 2.26MB
commons-fileupload-1.2.1.jar 56KB
commons-logging-api-1.1.jar 44KB
web.xml 827B
index.jsp 814B
META-INF
MANIFEST.MF 39B
.myeclipse
.classpath 911B
共 19 条
- 1
Blazzer
- 粉丝: 5
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页