package com.sict.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.sict.entity.User;
import com.sict.service.UsersService;
public class RegisterServlet extends HttpServlet {
private ServletConfig config;
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
this.config=config;
}
/**
* 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
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
//新建一个SmartUpload的对象
SmartUpload smartUpload = new SmartUpload();
try {
//对上传对象初始化
smartUpload.initialize(config, request, response);
//设置每个上传文件的最大长度 单位是字节B
smartUpload.setMaxFileSize(1024*1024*2);
//设置总上传数据的长度
//smartUpload.setTotalMaxFileSize(4*1024*1024);
//设置允许上传的文件类型
smartUpload.setAllowedFilesList("jpg,gif,png");
//设置禁止上传的文件类型
smartUpload.setDeniedFilesList("exe,bat");
//上传操作(客户端的文件 传到 服务器)
smartUpload.upload();
//如果是一个/,代表的就是当前工程的根目录
int count = smartUpload.save("/upload");
//获取用户名
String uname = smartUpload.getRequest().getParameter("uname");
//获取密码
String upwd = smartUpload.getRequest().getParameter("upwd");
String headImg = null;
User user = new User();
user.setUname(uname);
user.setUpwd(upwd);
UsersService service = new UsersService();
//方法一:(int i = 0 ;i<count;i++)
//正在使用的是方法二
for(int i = 0;i<smartUpload.getFiles().getCount();i++){
File file = smartUpload.getFiles().getFile(i);
if(!file.isMissing()){
//获取文件名
headImg="/upload/"+file.getFileName();
user.setHeadImg(headImg);
}
service.register(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
jsp+servlet 图片上传
共32个文件
java:6个
class:6个
jar:5个
需积分: 50 11 下载量 80 浏览量
2018-01-06
17:14:45
上传
评论
收藏 628KB ZIP 举报
温馨提示
jsp+servlet 图片上传包括所有数据库和包,已注册的形式来上传图片
资源推荐
资源详情
资源评论
收起资源包目录
文件上传.zip (32个子文件)
文件上传
jspsmartupload.jar 12KB
fileupload
.project 1KB
src
com
sict
service
UsersService.java 451B
dao
UserDAO.java 2KB
BaseDAO.java 1KB
entity
User.java 605B
servlet
RegisterServlet.java 3KB
DologinServlet.java 2KB
WebRoot
WEB-INF
classes
com
sict
service
UsersService.class 820B
dao
UserDAO.class 3KB
BaseDAO.class 2KB
entity
User.class 1KB
servlet
DologinServlet.class 2KB
RegisterServlet.class 3KB
lib
servlet-api.jar 130KB
jspsmartupload.jar 12KB
jsp-api.jar 75KB
sqljdbc4.jar 413KB
web.xml 1KB
upload
register.jsp 1KB
index.jsp 834B
showUserInfo.jsp 1KB
META-INF
MANIFEST.MF 39B
login.jsp 1KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 414B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 491B
.jsdtscope 522B
.classpath 1KB
test.sql 224B
共 32 条
- 1
资源评论
xinjinjing1284
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功