文件上传 图片查看

@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(""+textFiles+"");
    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();  
}  

你可能感兴趣的:(文件上传 图片查看)