package openapi.restcontroller.wechatapp.system; import com.obs.services.model.ObjectMetadata; import net.sf.json.JSONObject; import openapi.base.Controller; import openapi.base.SQLFactory; import openapi.base.data.Row; import openapi.base.data.Rows; import openapi.base.data.db.DBConnect; import openapi.base.parameter.ErrCode; import openapi.base.parameter.ErrModel; import openapi.base.parameter.parameter; import openapi.tools.obs.BucketFile; import org.apache.commons.lang.StringUtils; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; import p2.application.doclink.Docinfo; import p2.application.doclink.Doclinks; import p2.p2server.P2Server; import p2.pao.PaoRemote; import p2.pao.PaoSetRemote; import p2.util.P2Exception; import p2.webclient.system.controller.UploadFile; import p2.webclient.system.controller.UploadFileOSS; import p2.webclient.system.controller.Utility; import javax.ws.rs.POST; import javax.ws.rs.Path; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.Vector; public class docManage extends Controller { public docManage() { } public docManage(JSONObject content) { super(content); } public String upLoadDoc(InputStream uploadfileInputStream, FormDataContentDisposition uploadfile, String ownertable, String ownerid, String hrid, String description, String filenamestr) { // 获取文件名称 JSONObject resultObject = new JSONObject(); if (uploadfile == null) { resultObject.put("code", 0); resultObject.put("errcode", 0); resultObject.put("msg", "文件为空"); return resultObject.toString(); } String siteid = getSiteid(hrid); String doctype = "Attachments"; try { //获取文件名称 String filename = new String(uploadfile.getFileName().getBytes("ISO8859-1"), StandardCharsets.UTF_8); /** * 判断参数合法性 */ if (Utility.isNull(ownertable)) { resultObject.put("code", 0); resultObject.put("errcode", 0); resultObject.put("msg", "参数ownertable错误"); return resultObject.toString(); } if (Utility.isNull(ownerid)) { resultObject.put("code", 0); resultObject.put("errcode", 0); resultObject.put("msg", "参数ownerid错误"); return resultObject.toString(); } if (Utility.isNull(filename)) { resultObject.put("code", 0); resultObject.put("errcode", 0); resultObject.put("msg", "参数 uploadfile.getFileName()错误"); return resultObject.toString(); } if (Utility.isNull(siteid)) { resultObject.put("code", 0); resultObject.put("errcode", 0); resultObject.put("msg", "参数siteid错误"); return resultObject.toString(); } if (Utility.isNull(doctype)) { resultObject.put("code", 0); resultObject.put("errcode", 0); resultObject.put("msg", "参数doctype错误"); return resultObject.toString(); } /** * 将上传文件内容进行缓存操作 */ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { byte[] arrayOfByte = new byte[4096]; int i; while (-1 != (i = uploadfileInputStream.read(arrayOfByte))) { byteArrayOutputStream.write(arrayOfByte, 0, i); } } catch (IOException iOException) { } /** * 开始进行文件写入操作 */ UploadFile uploadFile = new UploadFile(filename, "image\\png", byteArrayOutputStream); //获取p2设置参数 Properties properties = P2Server.getP2Server().getConfig(); //获取附件文件夹路径 StringBuffer defaultpath = new StringBuffer(properties.getProperty("p2.attachment.defaultpath")); String ostype = properties.getProperty("p2.attachment.ostype"); //附件文件夹下相对路径 String filepath = ""; /** * 获取附件存放规则:站点?表?附件类型? */ PaoSetRemote ps_sysvarsSet = P2Server.getP2Server().getPaoSet("PS_SYSVARS", P2Server.getP2Server().getSystemUserInfo()); ps_sysvarsSet.setWhere("varname='DOCUMENT_DIR_RULE'"); ps_sysvarsSet.reset(); if (!ps_sysvarsSet.isEmpty()) { String[] document_dir_rule = ps_sysvarsSet.getPao(0).getString("varvalue") .split("[/]"); defaultpath.append(File.separator + siteid); /** * 根据文件存放规则生成相对路径和完整路径 */ filepath = filepath + siteid + "/"; for (byte b = 1; b < document_dir_rule.length; b++) { if ("DOCTYPE".equalsIgnoreCase(document_dir_rule[b])) { defaultpath.append(File.separator + doctype); filepath = filepath + doctype + "/"; } else if ("TABLE".equalsIgnoreCase(document_dir_rule[b])) { defaultpath.append(File.separator + ownertable); filepath = filepath + ownertable + "/"; } } } ps_sysvarsSet.close(); /** * 文件类型 */ String filetype = filename.substring(filename.lastIndexOf(".") + 1); if (!"".equals(filenamestr)) { filename = filenamestr; } try { /** * 创建文件夹,如文件夹不存在则自动创建文件夹 */ uploadFile.setDirectoryName(defaultpath.toString()); /** * 开始写入文件 */ uploadFile.writeToDisk(); /** * 获取文件完整路径(加上自动生成的文件流水码) */ String absolutefilename = uploadFile.getAbsoluteFileName(); File file = new File(absolutefilename); /** * 如果上传阿里云,则执行如下代码 */ if (ostype != null && !Utility.isNull(ostype.trim()) && ostype.trim().equalsIgnoreCase("OSS")) { UploadFileOSS uploadFileOSS = new UploadFileOSS(absolutefilename, uploadFile.getFileName(), "", filepath); uploadFileOSS.writeOSS(); if (file.exists() && file.isFile()) { file.delete(); } } /** * 保存文件 */ if (!Utility.isNull(absolutefilename)) { uploadFile.save(); } } catch (IOException iOException) { } PaoSetRemote docinfoSet = P2Server.getP2Server().getPaoSet("DOCINFO", P2Server.getP2Server().getSystemUserInfo()); PaoRemote docinfo = docinfoSet.add(); docinfo.setValue("document", filename, 11L); docinfo.setValue("ownertable", ownertable, 11L); docinfo.setValue("description", description, 11L); docinfo.setValue("postfix", filetype, 11L); docinfo.setValue("doctype", doctype, 11L); docinfo.setValue("serialnumber", uploadFile.getFileName(), 11L); docinfo.setValue("siteid", siteid, 11L); docinfoSet.save(); PaoSetRemote doclinksSet = docinfo.getPaoSet("DOCLINKS"); if (doclinksSet.isEmpty()) { PaoRemote doclinks = doclinksSet.add(); doclinks.setValue("document", filename, 11L); doclinks.setValue("docinfoid", docinfo.getLong("docinfoid"), 11L); doclinks.setValue("ownertable", ownertable, 11L); doclinks.setValue("DOCTYPE", doctype, 11L); doclinks.setValue("description", description, 11L); doclinks.setValue("OWNERID", ownerid, 11L); doclinksSet.save(); doclinksSet = doclinks.getPaoSet("$DOCHISTORY", "DOCHISTORY", ""); ((Doclinks) doclinks).addDochistory(doclinksSet, "Add"); doclinksSet.save(); } resultObject.put("code", 1); resultObject.put("msg", "成功"); resultObject.put("data", queryDocRows(ownertable, ownerid)); docinfoSet.close(); } catch (Exception exception) { resultObject.put("code", 0); resultObject.put("errcode", 0); resultObject.put("msg", exception.getMessage()); } return resultObject.toString(); } public String upLoadDocUrl(String urlstr, String ownertable, String ownerid, String hrid, String description) { JSONObject resultObject = new JSONObject(); String siteid = getSiteid(hrid); String doctype = "Attachments"; try { String str = urlstr.substring(urlstr.lastIndexOf("/") + 1); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { URL url = new URL(urlstr); HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection(); httpUrl.connect(); BufferedInputStream bis = new BufferedInputStream(httpUrl.getInputStream()); byte[] arrayOfByte = new byte[4096]; int i; while (-1 != (i = bis.read(arrayOfByte))) { byteArrayOutputStream.write(arrayOfByte, 0, i); } } catch (IOException iOException) { } UploadFile uploadFile = new UploadFile(str, "image\\png", byteArrayOutputStream); Properties properties = P2Server.getP2Server().getConfig(); StringBuffer stringBuffer = new StringBuffer(properties.getProperty("p2.attachment.defaultpath")); String str1 = properties.getProperty("p2.attachment.ostype"); String str2 = ""; PaoSetRemote ps_sysvarsSet = P2Server.getP2Server().getPaoSet("PS_SYSVARS", P2Server.getP2Server().getSystemUserInfo()); ps_sysvarsSet.setWhere("varname='DOCUMENT_DIR_RULE'"); ps_sysvarsSet.reset(); if (!ps_sysvarsSet.isEmpty()) { String[] arrayOfString = ps_sysvarsSet.getPao(0).getString("varvalue") .split("[/]"); stringBuffer.append(File.separator + siteid); str2 = str2 + siteid + "/"; for (byte b = 1; b < arrayOfString.length; b++) { if ("DOCTYPE".equalsIgnoreCase(arrayOfString[b])) { stringBuffer.append(File.separator + doctype); str2 = str2 + doctype + "/"; } else if ("TABLE".equalsIgnoreCase(arrayOfString[b])) { stringBuffer.append(File.separator + ownertable); str2 = str2 + ownertable + "/"; } } } ps_sysvarsSet.close(); String str3 = stringBuffer.toString(); String filename = uploadFile.getFileName(); String filetype = filename.substring(filename.lastIndexOf(".") + 1); filename = "url附件"; try { uploadFile.setDirectoryName(str3); uploadFile.writeToDisk(); str3 = uploadFile.getAbsoluteFileName(); File file = new File(str3); if (str1 != null && !Utility.isNull(str1.trim()) && str1.trim().equalsIgnoreCase("OSS")) { UploadFileOSS uploadFileOSS; (uploadFileOSS = new UploadFileOSS(str3, uploadFile.getFileName(), "", str2)).writeOSS(); if (file.exists() && file.isFile()) { file.delete(); } } if (!Utility.isNull(str3)) { uploadFile.save(); } } catch (IOException iOException) { } str3 = uploadFile.getFileName(); PaoRemote paoRemote; PaoSetRemote paoSetRemote3; (paoRemote = (paoSetRemote3 = P2Server.getP2Server().getPaoSet("DOCINFO", P2Server.getP2Server().getSystemUserInfo())).add()) .setValue("document", filename, 11L); paoRemote.setValue("ownertable", ownertable, 11L); paoRemote.setValue("description", description, 11L); paoRemote.setValue("postfix", filetype, 11L); paoRemote.setValue("doctype", doctype, 11L); paoRemote.setValue("serialnumber", str3, 11L); paoRemote.setValue("siteid", siteid, 11L); paoSetRemote3.save(); PaoSetRemote paoSetRemote1; if ((paoSetRemote1 = paoRemote.getPaoSet("DOCLINKS")).isEmpty()) { PaoRemote paoRemote1; (paoRemote1 = paoSetRemote1.add()).setValue("document", filename, 11L); paoRemote1.setValue("docinfoid", paoRemote.getLong("docinfoid"), 11L); paoRemote1.setValue("ownertable", ownertable, 11L); paoRemote1.setValue("DOCTYPE", doctype, 11L); paoRemote1.setValue("description", description, 11L); paoRemote1.setValue("OWNERID", ownerid, 11L); paoSetRemote1.save(); paoSetRemote1 = paoRemote1.getPaoSet("$DOCHISTORY", "DOCHISTORY", ""); ((Doclinks) paoRemote1).addDochistory(paoSetRemote1, "Add"); paoSetRemote1.save(); } paoSetRemote3.close(); resultObject.put("code", 1); resultObject.put("msg", "成功"); resultObject.put("data", queryDocRows(ownertable, ownerid)); } catch (Exception exception) { resultObject.put("code", 0); resultObject.put("msg", exception.getMessage()); } return resultObject.toString(); } public String deleteDoc() throws P2Exception { String ownertable = content.getString("ownertable"); String ownerid = content.getString("ownerid"); String docinfoid = content.getString("docinfoid"); // 获取文件名称 JSONObject resultObject = new JSONObject(); PaoSetRemote docinfoSet = P2Server.getP2Server().getPaoSet("DOCINFO", P2Server.getP2Server().getSystemUserInfo()); docinfoSet.setWhere("docinfoid ='" + docinfoid + "'"); docinfoSet.reset(); if (docinfoSet.isEmpty()) { resultObject.put("code", 0); resultObject.put("msg", "找不到id为" + docinfoid + "附件"); return resultObject.toString(); } docinfoSet.selectAll(); Vector vector = docinfoSet.getSelection(); String str = P2Server.getP2Server().getConfig().getProperty("p2.attachment.defaultpath"); PaoSetRemote ps_sysvarsSet = P2Server.getP2Server().getPaoSet("PS_SYSVARS", P2Server.getP2Server().getSystemUserInfo()); ps_sysvarsSet.setWhere("varname='DOCUMENT_DIR_RULE'"); ps_sysvarsSet.reset(); boolean bool1 = false; boolean bool2 = false; if (!ps_sysvarsSet.isEmpty()) { String[] arrayOfString = ps_sysvarsSet.getPao(0).getString("varvalue").split("[/]"); for (byte b = 1; b < arrayOfString.length; b++) { if ("DOCTYPE".equalsIgnoreCase(arrayOfString[b])) { bool1 = true; } else if ("TABLE".equalsIgnoreCase(arrayOfString[b])) { bool2 = true; } } } PaoSetRemote dochistorySet = P2Server.getP2Server().getPaoSet("DOCHISTORY", P2Server.getP2Server().getSystemUserInfo()); ArrayList arrayList = new ArrayList(); int j = vector.size(); for (byte b2 = 0; b2 < j; b2++) { PaoRemote paoRemote; if ((paoRemote = (PaoRemote) vector.elementAt(b2)) != null) { String str1 = "docinfoid = " + paoRemote.getLong("docinfoid") + " and ownertable='" + ownertable + "' and ownerid = '" + ownerid + "'"; PaoSetRemote paoSetRemote; PaoRemote paoRemote1 = (paoSetRemote = paoRemote.getPaoSet("$DOCLINKS", "DOCLINKS", str1)).getPao(0); if (((Docinfo) paoRemote).canBeDelete()) { String str3 = paoRemote.getString("serialnumber"); StringBuffer stringBuffer = new StringBuffer(str); String str2 = paoRemote.getString("siteid"); stringBuffer.append(File.separator + str2); if (bool1) { stringBuffer.append(File.separator + paoRemote.getString("doctype")); } if (bool2) { stringBuffer.append(File.separator + paoRemote.getString("ownertable")); } stringBuffer.append(File.separator + str3); str2 = stringBuffer.toString(); arrayList.add(str2); paoRemote.delete(); } ((Doclinks) paoRemote1).addDochistory(dochistorySet, "Delete"); paoSetRemote.deleteAll(2L); } } dochistorySet.save(); docinfoSet.deleteAll(); docinfoSet.save(); for (byte b1 = 0; b1 < arrayList.size(); b1++) { File file; if ((file = new File(arrayList.get(b1))).exists()) { file.delete(); } } dochistorySet.close(); docinfoSet.close(); ps_sysvarsSet.close(); resultObject.put("code", 1); resultObject.put("msg", "成功"); resultObject.put("data", queryDocRows(ownertable, ownerid)); return resultObject.toString(); } public String queryDoc() { String ownertable = content.getString("ownertable"); String ownerid = content.getString("ownerid"); Rows rows = queryDocRows(ownertable, ownerid); return getReturnObject_suc(rows, true).toString(); } private Rows queryDocRows(String ownertable, String ownerid) { DBConnect dbConnect = new DBConnect(); SQLFactory sqlFactory = new SQLFactory(this, "附件查询"); sqlFactory.addParameter("ownertable", ownertable); sqlFactory.addParameter("ownerid", ownerid); Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL()); return rows; } private String getSiteid(String hrid) { if (parameter.siteidList.containsKey(hrid)) { return parameter.siteidList.get(hrid); } DBConnect connect = new DBConnect(); Rows rows = connect.runSqlQuery("select defsite from pp_users where hrid='" + hrid + "'"); if (!rows.isEmpty()) { return rows.get(0).getString("defsite"); } return ""; } /**华为云上传测试**/ public String upLoadDocForOBS(InputStream uploadfileInputStream, FormDataContentDisposition uploadfile, String ownertable, String ownerid, String hrid, String description, String filenamestr,String type) throws IOException, P2Exception { DBConnect dbConnect=new DBConnect(); Rows rows = dbConnect.runSqlQuery("select defsite from pp_users where hrid='" + hrid + "'"); if(!rows.isEmpty()){ String siteid=rows.get(0).getString("defsite"); /**华为云上传**/ BucketFile bucketFile=new BucketFile(siteid); String ftype=uploadfile.getFileName().substring(uploadfile.getFileName().lastIndexOf(".")+1,uploadfile.getFileName().length()); String fileName = OBSDocManage.createMD5FileName(uploadfile.getFileName())+"."+ftype; boolean isimage=ftype.equalsIgnoreCase("png")|| ftype.equalsIgnoreCase("jpg")|| ftype.equalsIgnoreCase("jpeg")?true:false; bucketFile.upload(fileName,uploadfileInputStream,isimage); /**将上传信息写入附件信息**/ String obsurl_imageshort = ""; if (isimage) { obsurl_imageshort = bucketFile.getMinImageUrl(fileName); } String obsurl = bucketFile.getFileUrl(fileName); ObjectMetadata matedata = bucketFile.getObjectMetadata(fileName); PaoSetRemote tattachmentSet = P2Server.getP2Server().getPaoSet("tattachment", P2Server.getP2Server().getSystemUserInfo()); PaoRemote tattachment = tattachmentSet.addAtEnd(); tattachment.setValue("siteid", siteid); tattachment.setValue("ownertable", ownertable); tattachment.setValue("ownerid", ownerid); tattachment.setValue("ftype", "default"); tattachment.setValue("postfix", ftype); tattachment.setValue("serialnumber", fileName); tattachment.setValue("fdocument", uploadfile.getFileName()); tattachment.setValue("fobsurl", obsurl); if(!StringUtils.isBlank(type)) tattachment.setValue("type", type); tattachmentSet.save(); Row row=new Row(); row.put("fdocument",uploadfile.getFileName()); row.put("fobsurl",obsurl); row.put("tattachmentid",tattachment.getUniqueIDValue()); row.put("postfix",ftype); row.put("type",type); return getReturnObject_suc(row,false).toString(); // tattachment.setValue("docinfoid", docinfo.getUniqueIDValue()); } return getReturnObject_err("错误").toString(); } }