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 {
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
android源码包集合2
共19个文件
jar:7个
java:3个
class:3个
5星 · 超过95%的资源 需积分: 10 61 下载量 90 浏览量
2013-02-01
13:15:33
上传
评论 2
收藏 42.56MB RAR 举报
温馨提示
Android与js交互.rar Android中监听电话状态.rar Android之GPS定位.zip Android之Wifi学习教程.rar Android之用PopupWindow实现弹出菜单.rar android写的google map api 应用.rar android在wifi下手机与电脑的socket通信.rar Android应用开发揭秘源码 .zip Android有未接来电后处理(判断未接来电).rar Android游戏源码——忍者快跑.rar android源代码分析.rar android源码一个柱状图的Demo.zip android源码下载工具.zip android源码捏苍蝇.zip Android自动发送短信.rar Android自动开关机实现.rar Android远程登录含有loading登录效.zip Angle v1.0_2D游戏引擎.ZIP BOOK看遍所有UI控件.7z BrewClock闹钟.zip cellmap v2.0 基站查询定位导航系统 .rar DialogShow.rar dialog去除边框代码.rar Droid Wall 手机防火墙.zip FiveChess五子棋.zip Flashlight灯光.zip Gmail备份手机短信【源码】.zip hotel宾馆系统.zip http 演示Demo.zip
资源推荐
资源详情
资源评论
收起资源包目录
android源码包集合2.rar (19个子文件)
Demo
src
com
demo
http
HttpPostServlet.java 4KB
FilePostOne.java 2KB
HttpClientTest.java 2KB
.project 1006B
WebRoot
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 827B
classes
com
demo
http
HttpClientTest.class 3KB
FilePostOne.class 3KB
HttpPostServlet.class 4KB
lib
log4j.jar 347KB
commons-codec-1.3.jar 46KB
axis2-1.2.jar 2.26MB
commons-httpclient-3.1.jar 298KB
commons-io-1.2.jar 64KB
commons-fileupload-1.2.1.jar 56KB
commons-logging-api-1.1.jar 44KB
index.jsp 814B
.mymetadata 285B
.classpath 911B
.myeclipse
共 19 条
- 1
ququ904648
- 粉丝: 4
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页