直接上菜,然后在点评一波:
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<byte[]> 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<byte[]> responseEntityDownloadPost() throws Exception {
File file = new File("C:\\Users\\Administrator\\Desktop\\tt\\小儿喜.zip");
InputStream inputStream = new FileInputStream(file);
byte[] bytes = null;
bytes = readInputStream(inputStr