spring mvc 上传图片

spring mvc 上传图片

方式1.将图片存入硬盘指定路径 通过请求将图片内容写入输出流中显示在页面

public static void showPic(String pic,HttpServletResponse response) {
    try{
         InputStream inputStream = null;
         ServletOutputStream outputStream=response.getOutputStream();
         response.reset();
         response.setContentType("image/jpeg");   
         String savePath = "";
         // 系统文件路径
         String globalPath = FileUploadContext.getGlobalPath();
         if (globalPath == null) {

             globalPath = "D://software//";
         }     
         savePath = globalPath+pic;

         try {
             inputStream = new FileInputStream(savePath);

             int bufferSize = 4096;
             byte[] b = new byte[bufferSize];
             int n = 0;

             do {
                 n = inputStream.read(b);

                 if (n > 0) {
                     outputStream.write(b, 0, n);
                 }
             } while (n > 0);

             outputStream.flush();
         } finally {
             if (inputStream != null) {
                 inputStream.close();
             }
             if (outputStream != null) {
                 outputStream.close();
             }
         }
    }catch(IOException e){
        logger.error(e.getMessage());
    }
    }

public static String uploadPic(HttpServletRequest req , String errorPic){          
        MultipartFile file = ((DefaultMultipartHttpServletRequest) req).getFile("pic");
        String fileName = errorPic;
        if(file!=null && file.getSize() > 0){
            try{
                
                if(file.getSize()>10000000){    
                    throw new BaseException("上传失败:文件大小不能超过10M"); 
                }
                
                String picPath = FileUploadContext.getGlobalPath();

                fileName = FileUploadContext.nextFileName();
                int point = fileName.lastIndexOf(".");
                fileName = fileName.substring(0,point);
                
                String realFileName = file.getOriginalFilename();
                int len = realFileName.length();
                point = realFileName.lastIndexOf(".");
                String prefix = realFileName.substring(point,len);
                
                if(prefix.equals("")){    
                    throw new IllegalArgumentException("文件无扩展名!"); 
                }else if (prefix.equalsIgnoreCase(".jpg") 
                        || prefix.equalsIgnoreCase(".gif") 
                        || prefix.equalsIgnoreCase(".bmp")) {
                    // system.out.println("upload file type is " + prefix);
                    fileName = fileName + prefix ;
                } else {
                    throw new IllegalArgumentException("文件类型错误!");
                }
                
                String fileFullName = picPath + fileName;
                File destFile = new File(fileFullName);
                file.transferTo(destFile);
                
            }catch(Exception e){
                logger.error(e.getMessage()); 
                return fileName;
            }
        }
        return fileName;
    }

方式2:将图片存入数据库blob字段中(如表数据量较大不推荐使用)

public static byte[] savePic(ServletContext context, String fileName) {
        
        byte[] buf = null;//10k缓存
        try{ 
             String picPath = context.getRealPath("WEB-INF");
             int point = picPath.lastIndexOf(File.separator);
             picPath = picPath.substring(0,point);
             
             picPath = picPath + fileName;
             
             FileInputStream imgis = new FileInputStream(picPath);
             int n = 0;
             
             ByteArrayOutputStream out = new ByteArrayOutputStream(4096);     
             byte[] b = new byte[4096];
             
             try {
                 while ((n = imgis.read(b)) != -1) { 
                     out.write(b, 0, n); 
                 }
             } finally {
                 if (imgis != null) {
                     imgis.close();
                 }
             }
             buf = out.toByteArray(); 
        }catch(IOException e){
            logger.error(e.getMessage());
        }
        
        return buf;
    }

public static void showPic(HttpServletResponse response,byte[] b) {
        try{
             ServletOutputStream outputStream=response.getOutputStream();
             response.reset();
             response.setContentType("image/jpeg");   
    
             try {
                 outputStream.write(b, 0, b.length);
                 outputStream.flush();
             } finally {
                 if (outputStream != null) {
                     outputStream.close();
                 }
             }
        }catch(IOException e){
            logger.error(e.getMessage());
        }
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值