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读取本地文件照片并在response中回传
最新推荐文章于 2024-07-25 05:25:54 发布