首页>代码>spring boot通过JdbcTemplate操作Blob写入图片到数据库的简单实例>/BlobDemo/src/main/java/com/springboot/blob/controller/DownloadController.java
package com.springboot.blob.controller;
import com.springboot.blob.domain.Picture;
import com.springboot.blob.service.QueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.Optional;
/**
* @author Simon
*/
@RestController
@RequestMapping("/test")
public class DownloadController {
@Autowired
private QueryService queryService;
@RequestMapping(value = "/insert", method = RequestMethod.GET)
public String insertBlob() {
Picture picture = new Picture();
String path = System.getProperty("user.dir");
String fileName = "blob.png";
picture.setUrl(path + "\\" + fileName);
picture.setFilename(fileName);
picture.setPath(path);
int id = queryService.insertBlob(picture);
System.out.println("id = " + id);
return "SUCCESS : " + "[" + id + "]";
}
/**
* 根据应聘者id下载对应的pdf简介
*
* @param request
* @param response
* @param id
* @throws IOException
*/
@RequestMapping(value = "/download/{id}", method = RequestMethod.GET)
public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable Long id) throws IOException {
try {
BufferedInputStream bis;
BufferedOutputStream bos;
OutputStream fos = null;
InputStream fis = null;
try {
Picture moneyPO = queryService.getBlob(String.valueOf(id));
//获取blog文件流
byte[] contents = moneyPO.getData();
fis = new ByteArrayInputStream(contents);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
String fileName = moneyPO.getFilename();
String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
//这个单独写一个方法,这个是防止下载的文件名乱码用的
setFileDownloadHeader(request, response, fileName);
String contentType;
Optional<MediaType> mediaType = MediaTypeFactory.getMediaType(fileName);
if (extension.equals("msg")) {
contentType = mediaType.orElse(MediaType.TEXT_HTML).toString();
} else {
contentType = mediaType.orElse(MediaType.APPLICATION_OCTET_STREAM).toString();
}
response.setHeader("Content-Type", contentType);
int byteRead = 0;
byte[] buffer = new byte[8192];
//二进制读取
while ((byteRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, byteRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (null != fos) {
fos.close();
}
if (null != fis) {
fis.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//防止乱码
public static void setFileDownloadHeader(HttpServletRequest request, HttpServletResponse response, String fileName) throws SQLException, UnsupportedEncodingException {
String encodedfileName;
String agent = request.getHeader("USER-AGENT");
if (null != agent && -1 != agent.indexOf("MSIE")) {
encodedfileName = URLEncoder.encode(fileName, "UTF-8");
} else if (null != agent && -1 != agent.indexOf("Mozilla")) {
encodedfileName = new String(fileName.getBytes("GBK"), "iso-8859-1");
} else {
encodedfileName = URLEncoder.encode(fileName, "UTF-8");
}
response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedfileName + "\"");
}
}
最近下载更多
最近浏览更多
小陈不会JAVA
3月3日
暂无贡献等级
WBelong LV8
2023年12月25日
lironggang LV38
2023年3月28日
18120344519 LV4
2023年3月23日
微信网友_6206233028890624 LV2
2022年11月8日
codeSheep LV1
2022年11月7日
zzh1 LV7
2022年11月5日
895704241
2022年9月2日
暂无贡献等级
lee123321 LV22
2022年8月31日
好的好的 LV9
2022年8月8日

