java读取本地文件照片并在response中回传

package com.tts.leader.datachange.web.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author Alley zhao created on 2021/4/13.
 */
@Slf4j
@RequestMapping("dataChange")
@RestController
public class DataChangeController {

    //两种方法都可以,URL:http://localhost:8080/dataChange/getLocalPicture2?picture=2.png
    //通过response获取本地照片
    @GetMapping(value = "/getLocalPicture2")
    public void getLocalPicture2(String picture, HttpServletResponse response) {
        OutputStream out = null;
        InputStream input = null;
        try {
            String imgPath = "E:/RuanJian1110/各种中文软件/" + picture;
            File file = new File(imgPath);
            input = new FileInputStream(file);
            int len = input.available();
            byte[] bytes = new byte[len];
            input.read(bytes);
            input.close();
            out = response.getOutputStream();
            out.write(bytes);
            out.flush();
            out.close();
        } catch (Exception e) {
            try {//异常也要关闭Stream流
                out.close();
                input.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    }

    @GetMapping(value = "/getLocalPicture")
    public void getLocalPicture(String picture, HttpServletResponse response) {
        ServletOutputStream out = null;
        FileInputStream input = null;
        try {
            out = response.getOutputStream();
            File file = new File("E:/RuanJian1110/各种中文软件/" + picture);
            input = new FileInputStream(file);
            byte[] bytes = new byte[7168];
            int len;
            while ((len = input.read(bytes)) != -1) {
                out.write(bytes, 0, len);
            }
            input.close();
            out.close();
        } catch (Exception e) {
            try {
                input.close();//失败了也要关闭Stream
                out.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }

    }

}
你可以使用Java的IO流来读取本地图片文件,并将其写入HTTP响应。下面是一个简单的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ImageServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取图片文件路径 String imagePath = "path/to/your/image.jpg"; // 设置响应内容类型为图片 response.setContentType("image/jpeg"); // 读取图片文件 File imageFile = new File(imagePath); FileInputStream fis = new FileInputStream(imageFile); // 获取输出流 OutputStream out = response.getOutputStream(); // 写入响应 byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } // 关闭流 fis.close(); out.close(); } } ``` 在上面的示例,首先需要指定要读取的图片文件的路径。然后,通过设置`response.setContentType("image/jpeg")`将响应内容类型设置为图片。接下来,使用`FileInputStream`读取图片文件,并通过`response.getOutputStream()`获取输出流。最后,使用循环将读取的内容写入输出流,并关闭输入和输出流。 请注意,上述示例是基于Java Servlet的,适用于Web应用程序。如果你在其他环境使用Java,可能需要根据具体情况进行适当的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值