@RequestMapping(value = "/loan/doUpload", method = RequestMethod.POST)
// @ResponseBody
public String upload(@RequestParam(value = "file", required = false) MultipartFile file,HttpSession session, HttpServletResponse response)
throws IOException {
LoanFile loanFile = null;
try{
Customer customer = (Customer)session.getAttribute(Constants.SESSION_USER);
loanFile = loanService.doUpload(file, customer);
}catch(Exception e){
e.printStackTrace();
}
response.setContentType("text/html;charset=UTF-8");
String textFiles = JsonUtil.getJsonStringGeneric(loanFile);
response.getWriter().write("<span>"+textFiles+"</span>");
return null;
}
@RequestMapping(value = "/laon/download")
public void download(@RequestParam(value="id") int id, HttpServletResponse response){
LoanFile loanFile = null;
try{
loanFile = loanService.getLoanFileById(id);
loanService.doDownload(loanFile.getFilePath(), loanFile.getFileFullName(), response);
}catch(Exception e){
e.printStackTrace();
}
}
@RequestMapping(value = "/loan/getPhotoById")
public void getPhotoById (int id, final HttpServletResponse response){
LoanFile loanFile = loanService.getLoanFileById(id);
File file = new File(loanService.getRootPath()+loanFile.getFilePath());
if (file.exists())
{
FileInputStream out = null;
try
{
out = new FileInputStream(file);
byte[] data = new byte[loanFile.getFileSize()];
out.read(data);
response.setContentType("image/jpeg");
response.setCharacterEncoding("UTF-8");
OutputStream outputSream = response.getOutputStream();
InputStream in = new ByteArrayInputStream(data);
int len = 0;
byte[] buf = new byte[1024];
while ((len = in.read(buf, 0, 1024)) != -1) {
outputSream.write(buf, 0, len);
}
outputSream.close();
}catch(Exception e){
e.printStackTrace();
}finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
@RequestMapping(value = "/loan/getPhotoByIdandSize")
public void getPhotoById (int id, int width, int height, final HttpServletResponse response){
LoanFile loanFile = loanService.getLoanFileById(id);
File file = new File(loanService.getRootPath() + loanFile.getFilePath());
if (file.exists()) {
FileInputStream out = null;
try {
out = new FileInputStream(file);
byte[] data = new byte[loanFile.getFileSize()];
out.read(data);
data = scaleImage(data, width, height);
response.setContentType("image/jpeg");
response.setCharacterEncoding("UTF-8");
OutputStream outputSream = response.getOutputStream();
InputStream in = new ByteArrayInputStream(data);
int len = 0;
byte[] buf = new byte[1024];
while ((len = in.read(buf, 0, 1024)) != -1) {
outputSream.write(buf, 0, len);
}
outputSream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public static byte[] scaleImage(byte[] data, int width, int height) throws IOException {
BufferedImage buffered_oldImage = ImageIO.read(new ByteArrayInputStream(data));
int imageOldWidth = buffered_oldImage.getWidth();
int imageOldHeight = buffered_oldImage.getHeight();
double scale_x = (double) width / imageOldWidth;
double scale_y = (double) height / imageOldHeight;
double scale_xy = Math.min(scale_x, scale_y);
int imageNewWidth = (int) (imageOldWidth * scale_xy);
int imageNewHeight = (int) (imageOldHeight * scale_xy);
BufferedImage buffered_newImage = new BufferedImage(imageNewWidth, imageNewHeight, BufferedImage.TYPE_INT_RGB);
buffered_newImage.getGraphics().drawImage(buffered_oldImage.getScaledInstance(imageNewWidth, imageNewHeight, BufferedImage.SCALE_SMOOTH), 0, 0, null);
buffered_newImage.getGraphics().dispose();
ByteArrayOutputStream outPutStream = new ByteArrayOutputStream();
ImageIO.write(buffered_newImage, "jpeg", outPutStream);
return outPutStream.toByteArray();
}
文件上传 图片查看
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- TCP传输 TCP传输是怎样建立的呢? Socket和ServerSocket 建立客户端和服务器端 建立连接后,...
- 由于项目还在开发阶段,只能贴上部分代码,敬请谅解; 注意点: 在后台上传必须是调用此方法:uploadTaskWi...
- 1.项目中需要图片的异步上传,度娘之后发现有一个插件可以完成这个需求,即是:Uploadify 下面是这个异步上传...