ackage com.neuedu.servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import com.neuedu.pojo.Image;
import com.neuedu.pojo.User;
import com.neuedu.service.IService;
import com.neuedu.service.impl.ServiceImpl;
import com.neuedu.utils.FileUtil;
/**
* Servlet implementation class UpLoadImgServlet
*/
@WebServlet("/upLoadImgServlet")
@MultipartConfig(maxFileSize=20971520)
public class UpLoadImgServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UpLoadImgServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String savePath = request.getServletContext().getRealPath("/upLoadImpFile");
Part part=request.getPart("headfile");
String phone=request.getParameter("phone");
String header=part.getHeader("content-disposition");
String oriFileName=FileUtil.getFileName(header);
if(oriFileName.endsWith("JPG") || oriFileName.endsWith("PNG") || oriFileName.endsWith("jpg")|| oriFileName.endsWith("png")) {
//step1 获取原文件名后缀
String suffix = oriFileName.substring(oriFileName.lastIndexOf("."));
//避免上传后文件名称重复,使用时间+随机数重命名上传后的文件名
String fileName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
Random random = new Random();
for (int i = 0; i < 3; i++) {
fileName = fileName + random.nextInt(10);
}
fileName = fileName + suffix;
// step2 把文件写到指定路径
part.write(savePath + File.separator +fileName);
//img src=""
//step3 把文件名、用户名、密码等存到数据库
PrintWriter out = response.getWriter();
IService is=new ServiceImpl();
User user=is.getUserByPhone(phone);
Image img=new Image();
img.setUserid(user.getId());
img.setImgname(fileName);
int i=is.addImg(img);
if(i>0){
request.setAttribute("msg","上传成功");
request.getRequestDispatcher("/updateUser.jsp").forward(request, response);
}else{
request.setAttribute("msg","上传失败");
request.getRequestDispatcher("/").forward(request, response);
}
}else {
request.setAttribute("msg","请上传图片类型的文件!");
request.getRequestDispatcher("/").forward(request, response);
}
}
}
uploadservlet
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 本文以Sass做案例,Less的参考,基本配置大同小异。假如我们有一个Sass的全局变量common.scss文件...