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);


    }

    }

  • 相关阅读:
    【Html】Clipboard.js 实现点击复制,剪切板操作
    【转】【Python】python使用urlopen/urlretrieve下载文件时出现403 forbidden的解决方法
    【Html】div 加载 html页面的方法
    【WPF】创建文本字符串的路径PathGeometry
    【WPF】自定义鼠标样式
    Linux 错误记录
    微信开放平台代公众号管理
    微信开放平台获取授权公众号的流程
    vue-router "path" is required in a route configuration
    最大连接数“65535”的误解
  • 原文地址:https://www.cnblogs.com/liuzhaolong/p/12874557.html
Copyright © 2011-2022 走看看