起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

12
返回列表 发新帖

[处理中3] 上传图片报错

[复制链接]

91

主题

13万

帖子

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
35942
发表于 2018-1-5 14:37:27 | 显示全部楼层
把java代码完整的发一下
文档服务器中有报错
java.png
远程的联系方法QQ1392416607,添加好友时,需在备注里注明其论坛名字及ID,公司等信息
发远程时同时也发一下帖子地址,方便了解要解决的问题  WeX5教程  WeX5下载



如按照该方法解决,请及时跟帖,便于版主结贴
回复 支持 反对

使用道具 举报

17

主题

46

帖子

232

积分

中级会员

Rank: 3Rank: 3

积分
232
QQ
 楼主| 发表于 2018-1-5 14:45:37 | 显示全部楼层
public static int ArtPartReduceImage(String fID) throws Exception{
                String url = "/UI2/AppPro/ArtBookManager/process/ArtBookBillMaster/ImportFile";
                StringBuffer selectSql = new StringBuffer("");
                String updateSql = "";
                int isFl =0;
                selectSql.append("select fID,fImageStr as fImageStr1 from App_ArtProPartDetail(nolock) where fID = '"+fID+"'");
                Table table = BassClass.ExecSql(DATA_MODEL, selectSql.toString());
                for (Iterator<Row> it = table.iterator(); it.hasNext();) {
                        Row r = it.next();
                        String fImageStr = r.getString("fImageStr1");
                        fImageStr = GetImagerStrUrl2(fImageStr,url,200,160);
                        System.out.println(fImageStr);
                        if (fImageStr != null && !fImageStr.equals("")) {
                                updateSql = "UPDATE App_ArtProPartDetail set fImageStr='" + fImageStr + "',fIsReduce=1 where fID='" + fID + "' ";
                                HashMap<String, String> sqlMap = new HashMap<String, String>();
                                sqlMap.put("MSSQL", updateSql);
                                isFl= SQL.executeUpdate(sqlMap, null, DATA_MODEL);
                        }
                }
                return isFl;
        }
       
        public static String GetImagerStrUrl2(String fImagerStr,String url,int width,int height) throws Exception {
                String docID = "";
                String docPath = "";
                String time = "";
                String docName ="";
                String FilePath ="";
                String fParentPath = FileSystemWrapper.instance().getRealPath(url);// UI2/AppPro/PlanManager/process/ImportFile
                fParentPath = fParentPath.replace("BIZ/", ""); // 获取存储文件的UI2物理路径\\
       
                if (fImagerStr == null) {
                        fImagerStr = "";
                }
                if (!fImagerStr.equals("") && !fImagerStr.equals("[]")) {
                        JSONArray ja = new JSONArray(fImagerStr);
                        JSONObject jsonObject = ja.getJSONObject(0);
                        docPath = jsonObject.getString("docPath");
                        docID = jsonObject.getString("docID");
                        time = jsonObject.getString("time");
                        Docs docs = new Docs();
                        com.justep.doc.Doc doc = docs.query(null, docPath, null, null, null).get(docID);
                       
                        /**
                         * docs.get(docId); 你应该考虑 docId不再docs里面后返回的doc是null的情况
                         * lock的时候 已经有别人锁上了 根据返回值来判断是否已经锁上的情况。
                         * */
                        if(doc != null){
                                docName="";
                                docName = doc.getsDocName();
                                FilePath="";
                                FilePath =fParentPath+'/'+ docName;
                                File outPutFile = new File(FilePath);
                                doc.download(new FileOutputStream(outPutFile));
                                zipWidthHeightImageFile(new File(FilePath), new File(FilePath), width,height, 0.7f);       
                                docName="";
                                docName = doc.getsDocName();
                                doc = docs.addDoc("defaultDocNameSpace");
                                FilePath="";
                                FilePath =fParentPath+'/'+ docName;
                                File f = new File(FilePath);
                                doc.upload(f);
                                docs.createVersion();
                                FilePath = FilePath.replaceAll("\\\\", "/");
                                deleteFile(FilePath);//删除文件
                                String fImageStr = "[{\"docID\":\"" + doc.getsID() + "\",\"docName\":\"" + doc.getsDocName() + "\"," + "\"size\":\"" + doc.getsSize() + "\",\"docPath\":\"" + doc.getsDocPath() + "\","
                                                + "\"fileID\":\"" + doc.getsFileID() + "\",\"time\":\"" + time + "\"}]";
                                return fImageStr;
                        }
                }
                return "";

        }
       
        public static boolean deleteFile(String fileName) {
                File file = new File(fileName);
                // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
                if (file.exists() && file.isFile()) {
                        if (file.delete()) {
                                return true;
                        } else {
                                return false;
                        }
                } else {
                        return false;
                }
        }
       
        public static String zipImageFile(File oldFile,File newFile, int width, int height,float quality) {   
        if (oldFile == null) {   
            return null;   
        }   
        try {   
            /** 对服务器上的临时文件进行处理 */   
            Image srcFile = ImageIO.read(oldFile);   
            int w = srcFile.getWidth(null);   
            int h = srcFile.getHeight(null);   
            double bili;   
            if(width>0){   
                bili=width/(double)w;   
                height = (int) (h*bili);   
            }else{   
                if(height>0){   
                    bili=height/(double)h;   
                    width = (int) (w*bili);   
                }   
            }   
              
            String srcImgPath = newFile.getAbsoluteFile().toString();  
            String subfix = "jpg";  
            subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".")+1,srcImgPath.length());  
  
            BufferedImage buffImg = null;   
            if(subfix.equals("png")){  
                buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);  
            }else{  
                buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
            }  
  
            Graphics2D graphics = buffImg.createGraphics();  
            graphics.setBackground(new Color(255,255,255));  
            graphics.setColor(new Color(255,255,255));  
            graphics.fillRect(0, 0, width, height);  
            graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);   
  
            ImageIO.write(buffImg, subfix, new File(srcImgPath));   
   
        } catch (FileNotFoundException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        return newFile.getAbsolutePath();   
    }   
   
    /**  
     * 按设置的宽度高度压缩图片文件<br> 先保存原文件,再压缩、上传  
     * @param oldFile  要进行压缩的文件全路径  
     * @param newFile  新文件  
     * @param width  宽度  
     * @param height 高度  
     * @param quality 质量  
     * @return 返回压缩后的文件的全路径  
     */   
    public static String zipWidthHeightImageFile(File oldFile,File newFile, int width, int height,float quality) {   
        if (oldFile == null) {   
            return null;   
        }   
        String newImage = null;   
        try {   
            /** 对服务器上的临时文件进行处理 */   
            Image srcFile = ImageIO.read(oldFile);   
              
            String srcImgPath = newFile.getAbsoluteFile().toString();  
            String subfix = "jpg";  
            subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".")+1,srcImgPath.length());  
  
            BufferedImage buffImg = null;   
            if(subfix.equals("png")){  
                buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);  
            }else{  
                buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
            }  
  
            Graphics2D graphics = buffImg.createGraphics();  
            graphics.setBackground(new Color(255,255,255));  
            graphics.setColor(new Color(255,255,255));  
            graphics.fillRect(0, 0, width, height);  
            graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);   
  
            ImageIO.write(buffImg, subfix, new File(srcImgPath));   
        } catch (FileNotFoundException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        return newImage;   
    }     

这是完整的代码。 先下载文件。然后重新上传。最后删除。 还有一个问题就是图片存在的。但是文档中通过com.justep.doc.Doc doc = docs.query(null, docPath, null, null, null).get(docID); docID查找出来的是空的
回复 支持 反对

使用道具 举报

91

主题

13万

帖子

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
35942
发表于 2018-1-5 15:18:22 | 显示全部楼层
把要上传的文件通过文档中心上传看看
远程的联系方法QQ1392416607,添加好友时,需在备注里注明其论坛名字及ID,公司等信息
发远程时同时也发一下帖子地址,方便了解要解决的问题  WeX5教程  WeX5下载



如按照该方法解决,请及时跟帖,便于版主结贴
回复 支持 反对

使用道具 举报

17

主题

46

帖子

232

积分

中级会员

Rank: 3Rank: 3

积分
232
QQ
 楼主| 发表于 2018-1-5 15:33:02 | 显示全部楼层
jishuang 发表于 2018-1-5 15:18
把要上传的文件通过文档中心上传看看

u=2467151396,503955766&fm=21&gp=0.jpg 一样的报错 。这是图片的名称 u=2467151396,503955766&amp;fm=21&amp;gp=0.jpg
回复 支持 反对

使用道具 举报

91

主题

13万

帖子

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
35942
发表于 2018-1-5 16:35:04 | 显示全部楼层
应该是图片名中&fm解析有问题
把图片改一个名字

报错的就是fm
远程的联系方法QQ1392416607,添加好友时,需在备注里注明其论坛名字及ID,公司等信息
发远程时同时也发一下帖子地址,方便了解要解决的问题  WeX5教程  WeX5下载



如按照该方法解决,请及时跟帖,便于版主结贴
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2024-5-17 14:34 , Processed in 0.087559 second(s), 25 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表