zoukankan      html  css  js  c++  java
  • 双表的增删改查-上传图片工具类

    package com.lzl.controller;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.UUID;

    import javax.servlet.http.HttpServletResponse;

    import org.springframework.web.multipart.MultipartFile;

    public class FileUtils {

    /**
    *
    * @param response
    * @param file
    * @throws FileNotFoundException
    */
    public static void downLoad(HttpServletResponse response, String filename) throws FileNotFoundException {
    /* // 下载本地文件
    String fileName = "Operator.doc".toString(); // 文件的默认保存名
    */ // 读到流中
    InputStream inStream = new FileInputStream("d:\pic\"+filename);// 文件的存放路径
    // 设置输出的格式
    response.reset();
    response.setContentType("bin");
    response.addHeader("Content-Disposition", "attachment; filename="" + filename + """);

    // 循环取出流中的数据
    byte[] b = new byte[1024];
    int len;
    try {
    while ((len = inStream.read(b)) > 0)
    response.getOutputStream().write(b, 0, len);
    inStream.close();
    } catch (IOException e) {
    e.printStackTrace();
    }

    }


    /**
    * 上传文件
    * @param file
    * @return
    * @throws IllegalStateException
    * @throws IOException
    */
    public static String processFile(MultipartFile file) throws IllegalStateException, IOException {

    // 原来的文件名称
    System.out.println("file.isEmpty() :" + file.isEmpty() );
    System.out.println("file.name :" + file.getOriginalFilename());

    if(file.isEmpty()||"".equals(file.getOriginalFilename()) || file.getOriginalFilename().lastIndexOf('.')<0 ) {
    return "";
    }

    String originName = file.getOriginalFilename();
    String suffixName = originName.substring(originName.lastIndexOf('.'));
    SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");
    String path = "d:/pic/" + sdf.format(new Date());
    File pathFile = new File(path);
    if(!pathFile.exists()) {
    pathFile.mkdir();
    }
    String destFileName = path + "/" + UUID.randomUUID().toString() + suffixName;
    File distFile = new File( destFileName);
    file.transferTo(distFile);//文件另存到这个目录下边
    return destFileName.substring(7);


    }

    }

  • 相关阅读:
    从云计算到容器到容器云
    聊聊 CDN 缓存与浏览器缓存
    我眼中的 Nginx(六):深入 Nginx/Openresty 服务里的 DNS 解析
    HSTS 详解,让 HTTPS 更安全
    Polaristech 刘洋:基于 OpenResty/Kong 构建边缘计算平台
    Hadoop 2.7.4 HDFS+YRAN HA增加datanode和nodemanager
    hadoop HA学习
    Hadoop 2.7.4 HDFS+YRAN HA部署
    Grafana + Prometheus 监控PostgreSQL
    MySQL 导出用户权限
  • 原文地址:https://www.cnblogs.com/liuzhaolong/p/12874557.html
Copyright © 2011-2022 走看看