servlet 3.1 上传单张图片 package com.fz.servlet;import java.io.File;import java.io.IOException;import java.io.PrintWriter;import java.text.SimpleDateFormat;import java.util.Date;import java.util.UUID;import javax.enterprise.inject.New
package com.fz.servlet; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; import javax.enterprise.inject.New; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.Part; import com.fz.test.ImageText; @WebServlet("/up.do") @MultipartConfig public class Up extends HttpServlet { private static final long serialVersionUID = 1L; @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { String name = req.getParameter("name"); HttpServletRequest request = (HttpServletRequest) req; // 获取文件 Part part = request.getPart("ifile"); // 获取上传文件名字 String iString = part.getSubmittedFileName(); // 获取文件后缀名 String extString = iString.substring(iString.lastIndexOf(".")).toLowerCase(); // 设置文件路径 String pathString = this.getServletContext().getRealPath("upload/"); //缩略图文件路径 String path = this.getServletContext().getRealPath("uploadlim/"); //水印图标 String paths = this.getServletContext().getRealPath("65724_1408730_93.png"); //System.out.println(path+"-*"); File fi1 = new File(path); if (!fi1.exists()) { fi1.mkdirs(); } File file = new File(pathString); if (!file.exists()) { file.mkdirs(); } // UUID编码 String uuid = UUID.randomUUID().toString(); // 时间编码 SimpleDateFormat sFormat = new SimpleDateFormat("yyyyMMddhhmmssS"); String uuid1 = sFormat.format(new Date()); //上穿名(1)形式 //file.listFiles(); // 设置文件大小 long size = 1024 * 1000; if (part.getSize() < size) { System.out.println("----"); //文件上传 part.write(pathString+uuid1+extString); /*//批量图片加水印 File fis = new File(pathString); File[] f = fis.listFiles(); for (File file2 : f) { ImageText.writerText(file2); }*/ //单个图片加水印 File fi = new File(pathString+uuid1+extString); if (fi.exists()) { //添加水印文字 ImageText.writerText(fi); //生产缩略图 ImageText.littleImg(fi, new File(path+uuid+extString)); String uuid2 = UUID.randomUUID().toString(); //裁剪图片 ImageText.cutImg(fi, new File(path+uuid2+extString)); //logo ImageText.logo(fi,new File(paths)); } }else { System.out.println("文件太大"); PrintWriter out = res.getWriter(); out.println("文件太大"); } } }