zoukankan      html  css  js  c++  java
  • java 上传图片并把原图tagjson加回来

    public class FolderUtil {
        List<String> list = new ArrayList<String>(); 
        public  List<String> refreshFileList(String strPath) {  
            File dir = new File(strPath);  
            File[] files = dir.listFiles();  
            if (files == null)  
                return null;  
            for (int i = 0; i < files.length; i++) {  
                if (files[i].isDirectory()) {refreshFileList(
                        files[i].getAbsolutePath());
                } else {
                    String strFileName = files[i].getAbsolutePath();  
                    list.add(strFileName);  
                }  
            }  
            return list;  
        }  
     
        public  List<String> refreshFileList(String strPath,String str) {  
            File dir = new File(strPath);  
            File[] files = dir.listFiles();  
            if (files == null)  
                return null;  
            for (int i = 0; i < files.length; i++) {  
                if (files[i].isDirectory()) {refreshFileList(
                        files[i].getAbsolutePath(),str);
                } else {
                    String strFileName = files[i].getAbsolutePath();  
                    if(strFileName.indexOf(str)!=-1)
                    list.add(strFileName);  
                }  
            }  
            return list;  
        }  
    }
        public static String getFileMD5(InputStream inputStream){          
            String name="";  
            try {             
                byte[] bytes = new byte[1024];  
                int len = 0;                      
                MessageDigest messagedigest = MessageDigest.getInstance("MD5");           
                while ((len = inputStream.read(bytes)) > 0) {  
                    messagedigest.update(bytes, 0, len);  
                }  
                name = bufferToHex(messagedigest.digest());   
                inputStream.close();              
            } catch (MalformedURLException e) {  
            } catch (IOException e) {  
            } catch (NoSuchAlgorithmException e) {  
            }  
            return name;  
        }
        public void import4(){
            FolderUtil fu=new FolderUtil();
            List<String> imgList=fu.refreshFileList("D:\斑片影2", "jpg");
            int countadd=0;
            for(String imgUrl:imgList){
                TagImg  newImg=new TagImg();
                newImg.setOrgId("4e252d02fc0d4855909496c977b1ba86");
                newImg.setSetId("22636eb7d50447f39b24617fab3c8145");
                newImg.setTypeId("37ec21d7745f4f6eb05aba506bc14048");
                
                File imgFile = new File(imgUrl);
                //生成缩略图
                 InputStream in = null;
                 try {
                     in = new FileInputStream(imgFile);
                     String fileMd5Name=UploadFile.getFileMD5(in);
                     if(in!=null)
                         in.close();
                    
                     //TagImgFilter imgfilter=new TagImgFilter();                 
                     //imgfilter.setEq_imgMd5(fileMd5Name);
                    
                     
                     //取tag_json
                     TagImgFilter imgfilter2=new TagImgFilter();
                     String imgId0 = imgUrl.split("\\")[2];    //根据文件路径修改
                     String imgId1 = imgId0.split("\_")[1];
                     String imgId = imgId1.split("\.")[0];
                     //System.out.println(imgUrl);
                     System.out.println(imgId0);
                     System.out.println(imgId1);
                     System.out.println(imgId);
                     
                     //imgfilter2.setEq_id(imgId);    
                     imgfilter2.setEq_orgId("dd33751e9f9e464a9a8b2808d8aeb1cd");
                     imgfilter2.setEq_setId("aeb57e96903740f8ab35da63986d6e11");
                     imgfilter2.setEq_typeId("fc268bd0e55047199d43c9467f840c15");
                     imgfilter2.setEq_id(imgId);
                     //imgfilter2.setEq_imgMd5(imgId);//imgId为名字里md5值,此处没改名,之前是id值
                     TagImg tagImg = tagImgService.selectAll(imgfilter2).get(0);
                     
                     String tagJson = tagImg.getTagJson();
                     System.out.println(tagJson);            
                     
                     //上传原图
                     String opath=this.upFile(imgUrl, "jpg");
                     
                    /* JSONObject obj=new JSONObject();
                     obj.put("x", 1);
                     obj.put("y", 1);
                     
                        BufferedImage image = null;
                        try {
                             File wimgFile = new File(imgUrl);
                            image = ImageIO.read(wimgFile);
                            newImg.setThumbh(image.getHeight());
                            newImg.setThumbw(image.getWidth());
                             obj.put("width", image.getWidth()-1);
                             obj.put("height", image.getHeight()-1);
                            image = null;
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                     JSONArray ar=new JSONArray();
                     ar.add(obj);
                     newImg.setTagJson(ar.toJSONString());*/
                     newImg.setTagJson(tagJson);
                     newImg.setImgUrl(opath);
                     newImg.setImgMd5(fileMd5Name);
                     newImg.setTagFlag(1);
                     //缩略图
                     File simgFile=new File(imgUrl);
                     Map<String,Object> map=ImageZipUtil.zipImageFileByte(simgFile,310, 0, 1f);
                     byte[] bt = (byte[]) map.get("bt");
                     
                     ByteArrayInputStream bin = new ByteArrayInputStream(bt);
                     String spath= this.upFile(bin, bt.length, "jpg");
                     newImg.setImgUrlThumb(opath);
                     newImg.setCreateUser("originalbanpianying");
                     countadd++;
                     System.out.println(countadd);
                     bin.close();
                     bt=null;
                    // tagImgService.save(newImg);
                     
                     
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }
           
        public  String upFile(InputStream in,long fileSize,String extName) throws FileNotFoundException{
                StorePath storePath= fastFileStorageClient.uploadFile(in,fileSize,extName,null);
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    if(in!=null)
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }                
                }
                if(storePath!=null){
                    return storePath.getFullPath();
                }else
                    return null;          
        }
        
        public  String upFile(String path,String extName) throws FileNotFoundException{
            File file=new File(path);
            if(file.exists()){
                FileInputStream inputStream=new FileInputStream(file);
                StorePath storePath= fastFileStorageClient.uploadFile(inputStream,file.length(),extName,null);
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    if(inputStream!=null)
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }                
                }
                return storePath.getFullPath();
            }else{
                return null;
            }
            
      }
  • 相关阅读:
    C#中使用SendMessage在进程间传递数据的实例
    解决C# WINFORM程序只允许运行一个实例的几种方法详解
    实现WinForm窗体的美化(借助第三方控件)
    sql2008拒绝了对对象 (数据库 ,架构'dbo')的SELECT权限
    在一个SQL Server表中一行的多个列找出最大值
    如何搭建MVC + EF 框架
    灵活运用 SQL SERVER FOR XML PATH
    CentOS7切换到root用户和退回普通用户
    Zuul Read Time out 错误
    neo4j---删除关系和节点
  • 原文地址:https://www.cnblogs.com/lely/p/10364581.html
Copyright © 2011-2022 走看看