zoukankan      html  css  js  c++  java
  • 一个图片上传的servlet,传到本地磁盘,要传到服务器请修改

    本来想写个controller,结果拦截器把图片拦住了,那就直接servlet

    public class UploadEamge extends HttpServlet{

    /**
    *
    */
    private static final long serialVersionUID = 1L;
    private static final String[] IMAGE_TYPE = new String[] {".bmp",".jpeg",".jpg",".gif",".png"};

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    this.doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    // 校验图片格式
    DiskFileItemFactory factory = new DiskFileItemFactory();
    String uploadTempDir="E:/123";
    factory.setRepository(new File(uploadTempDir));
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setHeaderEncoding("utf-8");
    List<FileItem> fileList;
    try {
    fileList = upload.parseRequest(request);
    response.setCharacterEncoding("utf-8");
    String uuidName=null;
    Iterator<FileItem> it= fileList.iterator();
    String filePath="E:/123";
    File newFile = new File(filePath);
    if (!newFile.exists()) {
    newFile.mkdirs();// E:all_apachesapache-tomcat-6.0.32apache-tomcat-6.0.32webappsday09_uploadimg
    }
    while (it.hasNext()) {
    FileItem item = it.next();
    if (!item.isFormField()) {

    String name = item.getName();
    long size = item.getSize();

    if (name == null || name.trim().equals("")) {
    continue;
    }
    try {
    if (size > 716800) {
    response.getWriter()
    .print("{"returnMessage":"图片大小不能大于700K.","returnCode":"1024","imgUrl":""}");

    }
    } catch (Exception e) {
    e.getStackTrace();
    }
    boolean isLegal = false;
    for (String type : IMAGE_TYPE) {
    if (StringUtils.endsWithIgnoreCase(item.getName(), type)) {
    isLegal = true;
    System.out.println(isLegal);
    break;
    }
    System.out.println(isLegal);
    }

    try {
    uuidName=generateRandonFileName(item.getName());
    item.write(new File(newFile, uuidName));
    item.delete();
    } catch (Exception e) {
    e.printStackTrace();
    throw new RuntimeException();
    }
    }


    String url = "http://image.brand.com"+ "/"+uuidName;
    System.out.println(url);
    response.getWriter().print(
    "{"returnMessage":"","returnCode":"0000","imgUrl":""
    + url + ""}");

    }


    }catch (Exception e) {
    e.getStackTrace();
    }

    }
    public static String generateRandonFileName(String fileName) {
    String ext = fileName.substring(fileName.lastIndexOf("."));
    return UUID.randomUUID().toString() + ext;
    }

    }

  • 相关阅读:
    Maven的声明周期(Lifecycle )和命令(Phase)
    Java并发之线程异常捕获
    Java并发之需要了解但不能太依赖的东东
    ejs使用
    node.js BootStrap安装
    div+css关于overflow 动态滚动效果
    myBatis 参数配置
    jQuery Ajax请求提交 后台getParameter接收不到数据
    mysql+mybatis 插入可递增字段库表操作
    CSS浮动讲解好文章推荐
  • 原文地址:https://www.cnblogs.com/zhaoblog/p/5360201.html
Copyright © 2011-2022 走看看