servlet实现图片上传和下载

图片上传
前端
在这里插入图片描述
后端

protected void dopost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Part filePart = request.getPart("file"); // 获取上传的文件
        String fileName = filePart.getSubmittedFileName(); // 获取文件名

        // 将文件保存到指定位置
        File uploads = new File(request.getServletContext().getRealPath("/img"));
        File file = new File(uploads, fileName);
        try (InputStream input = filePart.getInputStream();
             OutputStream output = new FileOutputStream(file)) {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = input.read(buffer)) != -1) {
                output.write(buffer, 0, bytesRead);
            }
        }
        }

注意:
需要再servlet类上加上注解
File uploads = new File(request.getServletContext().getRealPath(“/img”));
/img为web目录下img目录

@MultipartConfig(maxFileSize = 5*1024*1024)

否则会出现
Part filePart = request.getPart(“file”);
获取为null


图片下载

 protected void dopost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path=request.getParameter("src");
        path = path.substring(4, path.length());
        String imageFilePath = request.getServletContext().getRealPath("/img") + "\\" + path; // 图片文件路径

        File imageFile = new File(imageFilePath);

        if (!imageFile.exists()) {
            response.getWriter().write("File not found");
            return;
        }
        response.setContentType("image/jpeg");
        response.setHeader("Content-Disposition", "attachment; filename=\""+path+ "\"");
        //获取下载文件的输入流
        FileInputStream in = null;
        ServletOutputStream out = null;
        try {
            in = new FileInputStream(imageFile);
            //创建缓冲区域
            int len = 0;
            byte[] buffer = new byte[1024];
            //获取输出流
            out = response.getOutputStream();
            while ((len=in.read(buffer)) > 0){
                out.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null){
                in.close();
            }
            if (out != null){
                out.close();
            }
        }
    }

相关推荐

  1. 使用.net core MVC实现图片下载

    2024-06-08 02:14:01       28 阅读
  2. uniapp实现文件图片选择功能实现

    2024-06-08 02:14:01       13 阅读
  3. axios 实现下载

    2024-06-08 02:14:01       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-08 02:14:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-08 02:14:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-08 02:14:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-08 02:14:01       20 阅读

热门阅读

  1. centos7如何备份

    2024-06-08 02:14:01       5 阅读
  2. docker的安装及docker常用命令

    2024-06-08 02:14:01       7 阅读
  3. 新一代AI的崛起——GPT-4o深度评析

    2024-06-08 02:14:01       8 阅读
  4. Debezium日常分享系列之:Debezium 2.7.0.Beta1发布

    2024-06-08 02:14:01       6 阅读
  5. 面试题--this关键字

    2024-06-08 02:14:01       10 阅读
  6. Vue3视图渲染技术

    2024-06-08 02:14:01       6 阅读
  7. Python怎么把数据从CSV文件导入到MySQL数据库?

    2024-06-08 02:14:01       12 阅读
  8. 【Qt快速入门(二)】- Qt 整体目录结构

    2024-06-08 02:14:01       12 阅读
  9. 深入探讨Qt中的QVariant

    2024-06-08 02:14:01       10 阅读
  10. ChatGPT的基本原理

    2024-06-08 02:14:01       8 阅读
  11. 是时候让临床预测模型进入临床实践

    2024-06-08 02:14:01       10 阅读