图片上传下载工具类

图片上传下载工具类

package com.gdh.unit;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class FileUploadUtil {
    //上传图片
    public static String uploadPhoto(MultipartFile loadimg) {
        //获取绝对路径
        String path = "D:/upload";
        File f = new File(path);
        //如果不存在,直接创建
        if (!f.exists()) {
            f.mkdirs();
        }
        //获取图片名称
        String filename = loadimg.getOriginalFilename();
        System.out.println(filename);
        //拼接的图片路径
        String filepath = path + "/" + filename;
        File file = new File(filepath);
        System.out.println(file + "这是图片保存的路径");
        //上传图片
        try {
            loadimg.transferTo(file);
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return filepath;
    }

    //回显图片
    public static void showPhoto(String photo, HttpServletResponse response) throws IOException {
        //获取图片的当前路径  放入读
        FileInputStream fis = null;
        //用response  获取一个写对象的流
        ServletOutputStream os = null;
        try {
            fis = new FileInputStream(photo);
            os = response.getOutputStream();
            //提高读写的速度
            byte[] b = new byte[1024];
            //边读边写
            while (fis.read(b) != -1) {
                os.write(b);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

    //下载
    public static void downLoad(String filepath, HttpServletRequest request, HttpServletResponse response) {
        //设置文件的MiMe类型
        response.setContentType(request.getSession().getServletContext().getMimeType(filepath));
        //设置content-dispsition
        response.setHeader("Content-Disposition", "attachment;filename=" + filepath);
        //读取目标文件,通过response将目标文件写到客户
        try {
            //读取文件
            InputStream in = new FileInputStream(filepath);
            OutputStream out = response.getOutputStream();
            //写文件
            byte[] b = new byte[1024];
            while (in.read(b) != -1) {
                out.write(b);
            }
            in.close();
            out.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曾经不在

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值