uploadservlet

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)