直接上菜,然后在点评一波:
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
@RestController
@RequestMapping(value = "/test")
public class KeyController {
/**
* ResponseEntity图片预览
*
* @return
* @throws Exception
*/
@GetMapping("/responseEntityView")
public ResponseEntity responseEntityView() throws Exception {
File file = new File("E:\\图片\\bpic4828.jpg");
InputStream inputStream = new FileInputStream(file);
byte[] bytes = null;
bytes = readInputStream(inputStream);
HttpHeaders httpHeaders = new HttpHeaders();
// 不是用缓存
httpHeaders.setCacheControl(CacheControl.noCache());
httpHeaders.setPragma("no-cache");
httpHeaders.setExpires(0L);
httpHeaders.setContentType(MediaType.IMAGE_JPEG);
ResponseEntity responseEntity = new ResponseEntity<>(bytes, httpHeaders, HttpStatus.OK);
return responseEntity;
}
/**
* ResponseEntity文件下载Post请求
* (如果不会参数不多就最好使用get,post请求不会解码文件名,如果不编码中文就会成一个_,没有中文用那个都可以)
*
* @return
* @throws Exception
*/
@PostMapping("/responseEntityDownloadPost")
public ResponseEntity responseEntityDownloadPost() throws Exception {
File file = new File("C:\\Users\\Administrator\\Desktop\\tt\\小儿喜.zip");
InputStream inputStream = new FileInputStream(file);
byte[] bytes = null;
bytes = readInputStream(inputStream);
String fileName = URLEncoder.encode(file.getName(), "UTF-8");
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentDispositionFormData("attachment", fileName);
httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
ResponseEntity responseEntity = new ResponseEntity<>(bytes, httpHeaders, HttpStatus.OK);
return responseEntity;
}
/**
* ResponseEntity文件下载Get请求
*
* @return
* @throws Exception
*/
@GetMapping("/responseEntityDownloadGet")
public ResponseEntity responseEntityDownloadGet() throws Exception {
File file = new File("C:\\Users\\Administrator\\Desktop\\tt\\小儿喜.zip");
InputStream inputStream = new FileInputStream(file);
byte[] bytes = null;
bytes = readInputStream(inputStream);
String fileName = URLEncoder.encode(file.getName(), "UTF-8");
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentDispositionFormData("attachment", fileName);
httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
ResponseEntity responseEntity = new ResponseEntity<>(bytes, httpHeaders, HttpStatus.OK);
return responseEntity;
}
/**
* ResponseEntity文件下载Get请求PostPost请求不会自动解码,所以使用get请求转一下就没有问题了
*
* @return
* @throws Exception
*/
@GetMapping("/responseEntityDownloadGetByPost")
public ResponseEntity responseEntityDownloadGetByPost() throws Exception {
ResponseEntity responseEntity = this.responseEntityDownloadPost();
return responseEntity;
}
/**
* HttpServletResponse图片预览
*
* @param response
* @return
* @throws Exception
*/
@GetMapping("/httpServletResponseView")
public String httpServletResponseView(HttpServletResponse response) throws Exception {
File file = new File("E:\\图片\\bpic4828.jpg");
InputStream inputStream = new FileInputStream(file);
byte[] bytes = null;
OutputStream outputStream = response.getOutputStream();
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
// 不使用缓存
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("expires", -1);
response.setHeader("Content-Disposition", "inline");
readInputStreamToOutStream(inputStream, outputStream);
return null;
}
/**
* HttpServletResponse文件下载Post请求
*
* @param response
* @return
* @throws Exception
*/
@PostMapping("/httpServletResponseDownloadPost")
public String httpServletResponseDownloadPost(HttpServletResponse response) throws Exception {
File file = new File("C:\\Users\\Administrator\\Desktop\\tt\\小儿喜.zip");
InputStream inputStream = new FileInputStream(file);
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setCharacterEncoding("UTF-8");
OutputStream outputStream = response.getOutputStream();
String fileName = URLEncoder.encode(file.getName(), "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
readInputStreamToOutStream(inputStream, outputStream);
return null;
}
/**
* HttpServletResponse文件下载Get请求
*
* @param response
* @return
* @throws Exception
*/
@GetMapping("/httpServletResponseDownloadGet")
public String httpServletResponseDownloadGet(HttpServletResponse response) throws Exception {
File file = new File("C:\\Users\\Administrator\\Desktop\\tt\\小儿喜.zip");
InputStream inputStream = new FileInputStream(file);
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setCharacterEncoding("UTF-8");
OutputStream outputStream = response.getOutputStream();
String fileName = URLEncoder.encode(file.getName(), "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
readInputStreamToOutStream(inputStream, outputStream);
return null;
}
/**
* HttpServletResponse文件下载Get请求通过Post生成数据
* @param response
* @return
* @throws Exception
*/
@GetMapping("/httpServletResponseDownloadGetByPost")
public String httpServletResponseDownloadGetByPost(HttpServletResponse response) throws Exception {
this.httpServletResponseDownloadPost(response);
return null;
}
/**
* 输入流装成字节数组
*
* @param inStream
* @return
* @throws Exception
*/
private byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
/**
* 输入流写进输出流
*
* @param inStream
* @param outStream
* @throws Exception
*/
private void readInputStreamToOutStream(InputStream inStream, OutputStream outStream) throws Exception {
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
outStream.close();
}
}
1、HttpServletResponse 和 ResponseEntity的使用主要看个人了,如果使用Spring的项目就可以选择使用ResponseEntity,没有用Spring就可以直接使用HttpServletResponse。
2、不管用那个来下载文件,如果这个文件名是中文就要编码(不编码中文会变成_,没有中文就可以不编码的哦),但是编码也有一个问题,post请求不会自动解码,目前我是没有找到解决的方法(如果您有就可以写在评论区),get就会自动解码,所以有时候后台直接可以使用post请求(需要组装后台其他服务的多个参数的时候),前端获取的使用用get转一下就可以了。建议尽量使用get请求。
3、ResponseEntity其实可以看做是把HttpServletResponse封装了,但是功能比HttpServletResponse多,推介使用。
4、contentType的类型如果没有具体的类型就可以使用"application/octet-stream(MediaType.APPLICATION_OCTET_STREAM)",如果有具体类型建议使用具体类型。
5、如有不对请在评论区进行评论,谢谢。
《………………………………………………菜鸟起飞中,请各位走过路过的多多指教……………………………………》