import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
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;
/*处理 上传文件和普通表单的 servlet
*
* @author(陈烘)
* */
public class formservlet extends HttpServlet {
public String getStr(String str)
{
String s=null ;
if(str!=null)
{
try {
s = new String(str.getBytes("ISO8859-1"),"gb2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("转码出现错误");
}
}
return s;
}
/**
* 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
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
//设置合法的后缀名
final String[] allowedatt = new String[] {"doc","docx","html","htm"};
//创建该对象
DiskFileItemFactory factory = new DiskFileItemFactory();
//上传文件所用到的缓冲区大小,超过此缓冲区的部分将被写入到磁盘
factory.setSizeThreshold(4*1024);
//使用fileItemFactory为参数实例化一个ServletFileUpload对象
ServletFileUpload upload = new ServletFileUpload(factory);
//要求上传的form-data数据不超过1000M
upload.setSizeMax(100*1024*1024);
//java.util.List实例来接收上传的表单数据和文件数据
List itemList;
try {
itemList = upload.parseRequest(request);
//迭代list中的内容
Iterator it = itemList.iterator();
//循环list中的内容
while(it.hasNext())
{
//取出一个表单域
FileItem item =(FileItem) it.next();
//如果表单域是普通表单
if(item.isFormField())
{
//取出表单域的name,对应input的name
String name = (String)item.getFieldName();
//如果name的值是username
if("username".equals(name))
{
//输出username表单中的值
System.out.println(getStr(item.getString().trim()));
}
}
//如果不是普通表单,则代表是文件上传的表单
else
{
//获取上传文件的名字
String totalNameatt = getStr(item.getName());
//获取文件的后缀名
String suffixatt = totalNameatt.substring(totalNameatt.lastIndexOf(".") + 1);
//判断后缀名是否是我们允许上传的类型
boolean isValide=false;
for(int i=0;i<allowedatt.length;i++) {
if(suffixatt.equals(allowedatt[i])){
isValide=true;
}
}
//如果是不允许的类型,输出错误信息
if(!isValide)
{
System.out.println(totalNameatt+"该文件类型不在允许上传的范围内");
return ;
}
//如果是合法的,就保存该文件,文件名不变
else
{
//获取项目根目录
String rootPath = this.getServletConfig().getServletContext().getRealPath("/");
//要存储的文件的绝对路径
String file = rootPath +System.currentTimeMillis()+"."+suffixatt;
item.write(new File(file));
System.out.println("文件上传成功:"+file);
}
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
jsp,servlet普通表单+文件上传
共21个文件
jar:4个
ds_store:4个
xml:2个
5星 · 超过95%的资源 需积分: 16 74 下载量 27 浏览量
2015-01-17
19:10:29
上传
评论
收藏 495KB ZIP 举报
温馨提示
使用 jsp和servlet ,完成一个表单内的 普通表单和文件上传功能
资源推荐
资源详情
资源评论
收起资源包目录
FileUploadDemo.zip (21个子文件)
FileUploadDemo
.DS_Store 6KB
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 403B
org.eclipse.wst.common.component 493B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 510B
org.eclipse.jdt.core.prefs 357B
src
formservlet.java 4KB
.DS_Store 6KB
.project 1KB
WebRoot
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 698B
.DS_Store 6KB
classes
formservlet.class 4KB
lib
commons-fileupload.jar 87KB
commons-logging-1.1.3.jar 61KB
commons-codec-1.6.jar 227KB
commons-io-2.4.jar 181KB
.DS_Store 6KB
index.jsp 855B
.classpath 1KB
共 21 条
- 1
资源评论
- yangjingdu2015-09-10下载了觉得给自己很大帮组
- trfeiky2016-04-21下载不了。浪费分数
- tinalucy672019-03-07谢谢分享!
- dj9536908352017-04-11下载了觉得给自己很大帮组
藏红
- 粉丝: 101
- 资源: 50
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功