|
楼主 |
发表于 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查找出来的是空的 |
|