直接貼代碼吧
package com.wjs.controller;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
/**
* 獲取流程圖
*/
@Slf4j
@RequestMapping("/image")
@RestController
public class ImageController {
@Autowired
private RepositoryService repositoryService;
@RequestMapping(value = "/getImg",method = RequestMethod.GET)
public void getImg(@RequestParam String deploymentId, @RequestParam String resourceName, HttpServletRequest request, HttpServletResponse response){
log.info("deploymentId:{},resourceName:{}",deploymentId,resourceName);
response.setCharacterEncoding(request.getCharacterEncoding());
response.setContentType("application/octet-stream");
InputStream is = null;
is = repositoryService.getResourceAsStream(deploymentId, resourceName);
response.setHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode("流程圖.png"));
OutputStream os = null;
try {
// 這兩行代碼可以代替42-48行的代碼。
// IOUtils.copy(is,response.getOutputStream());
// response.flushBuffer();
os = response.getOutputStream();
byte[] b = new byte[1024];
int len;
while ((len = is.read(b)) != -1) {
os.write(b,0,len);
}
response.flushBuffer();
} catch (IOException e) {
e.printStackTrace();
log.error("圖片下載失敗....");
}finally {
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者