吴志根 4 лет назад
Родитель
Сommit
03f9eecad0

+ 78 - 0
src/dsb/com/cnd3b/restcontroller/system/system/OSSManage.java

@@ -0,0 +1,78 @@
+package com.cnd3b.restcontroller.system.system;
+
+import com.alibaba.fastjson.JSONObject;
+import com.aliyun.oss.OSS;
+import com.aliyun.oss.OSSClientBuilder;
+import com.aliyun.oss.model.*;
+import com.cnd3b.common.Controller;
+import com.cnd3b.utility.FileTypeHelper;
+import p2.pao.PaoRemote;
+import p2.pao.PaoSetRemote;
+import p2.util.P2Exception;
+
+import java.io.*;
+
+
+public class OSSManage extends Controller {
+
+    String endpoint = "https://oss-cn-beijing.aliyuncs.com";
+    String file_url = "https://jiusheng11.oss-cn-beijing.aliyuncs.com";
+    String accessKeyID = "LTAI5tASc17g95ABAqwUAqNS";
+    String accessKeySecret = "UUs8gERP91X8HGeWgeOa6jsAIGQhhK";
+    String bucketName = "jiusheng11";
+
+
+    /**
+     * 构造函数
+     *
+     * @param content
+     */
+    public OSSManage(JSONObject content) {
+        super(content);
+    }
+
+    public String uploadFile() throws P2Exception {
+        String filePath = content.getString("filePath");
+        String ownertable = content.getString("ownertable");
+        String ownerid = content.getString("ownerid");
+        String ftype = content.getString("ftype");
+
+        File file = new File(filePath);
+        if (!file.exists()) {
+            return getErrReturnObject().setErrMsg("文件不存在").toString();
+        }
+
+
+        //上传文件到阿里云
+        String filename = System.currentTimeMillis() + "-" + file.getName();
+        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyID, accessKeySecret);
+        ossClient.putObject(new PutObjectRequest(bucketName, filename, file));
+        ossClient.shutdown();
+        String url = file_url + "/" + filename;
+
+        //保存信息到附件表
+        PaoSetRemote paoSetRemote = getP2ServerSystemPaoSet("tattachment");
+        PaoRemote paoRemote = paoSetRemote.addAtEnd();
+        Long tattachmentid = paoRemote.getUniqueIDValue();
+        paoRemote.setValue("siteid", siteid, 11L);
+        paoRemote.setValue("ownertable", ownertable, 11L);
+        paoRemote.setValue("ownerid", ownerid, 11L);
+        paoRemote.setValue("createby", username, 11L);
+        paoRemote.setValue("ftype", ftype, 11L);
+        paoRemote.setValue("postfix", FileTypeHelper.getFileByFile(file), 11L);
+        paoRemote.setValue("serialnumber", filename, 11L);
+        paoRemote.setValue("fdocument", file.getName(), 11L);
+        paoRemote.setValue("fobsurl", url, 11L);
+        paoRemote.setValue("contentlength", String.valueOf(file.length()), 11L);
+        paoSetRemote.save();
+
+        JSONObject object = new JSONObject();
+        object.put("tattachmentid", tattachmentid);
+        object.put("fobsurl", url);
+
+        return getSucReturnObject().setData(object).toString();
+
+    }
+
+
+}

+ 196 - 0
src/dsb/com/cnd3b/utility/FileTypeHelper.java

@@ -0,0 +1,196 @@
+package com.cnd3b.utility;
+
+import javax.imageio.ImageIO;
+import javax.imageio.ImageReader;
+import javax.imageio.stream.ImageInputStream;
+import java.awt.image.BufferedImage;
+import java.io.*;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+public class FileTypeHelper {
+    public final static Map<String, String> FILE_TYPE_MAP = new HashMap<String, String>();
+
+    private FileTypeHelper() {
+    }
+
+    static {
+        getAllFileType();  //初始化文件类型信息
+    }
+
+    /**
+     * Created on 2010-7-1
+     * <p>Discription:[getAllFileType,常见文件头信息]</p>
+     *
+     * @author:[shixing_11@sina.com]
+     */
+    private static void getAllFileType() {
+        FILE_TYPE_MAP.put("jpg", "FFD8FF"); //JPEG (jpg)
+        FILE_TYPE_MAP.put("png", "89504E47");  //PNG (png)
+        FILE_TYPE_MAP.put("gif", "47494638");  //GIF (gif)
+        FILE_TYPE_MAP.put("tif", "49492A00");  //TIFF (tif)
+        FILE_TYPE_MAP.put("bmp", "424D"); //Windows Bitmap (bmp)
+        FILE_TYPE_MAP.put("dwg", "41433130"); //CAD (dwg)
+        FILE_TYPE_MAP.put("html", "68746D6C3E");  //HTML (html)
+        FILE_TYPE_MAP.put("rtf", "7B5C727466");  //Rich Text Format (rtf)
+        FILE_TYPE_MAP.put("xml", "3C3F786D6C");
+        FILE_TYPE_MAP.put("zip", "504B0304");
+        FILE_TYPE_MAP.put("rar", "52617221");
+        FILE_TYPE_MAP.put("psd", "38425053");  //Photoshop (psd)
+        FILE_TYPE_MAP.put("eml", "44656C69766572792D646174653A");  //Email [thorough only] (eml)
+        FILE_TYPE_MAP.put("dbx", "CFAD12FEC5FD746F");  //Outlook Express (dbx)
+        FILE_TYPE_MAP.put("pst", "2142444E");  //Outlook (pst)
+        FILE_TYPE_MAP.put("xls", "D0CF11E0");  //MS Word
+        FILE_TYPE_MAP.put("doc", "D0CF11E0");  //MS Excel 注意:word 和 excel的文件头一样
+        FILE_TYPE_MAP.put("mdb", "5374616E64617264204A");  //MS Access (mdb)
+        FILE_TYPE_MAP.put("wpd", "FF575043"); //WordPerfect (wpd)
+        FILE_TYPE_MAP.put("eps", "252150532D41646F6265");
+        FILE_TYPE_MAP.put("ps", "252150532D41646F6265");
+        FILE_TYPE_MAP.put("pdf", "255044462D312E");  //Adobe Acrobat (pdf)
+        FILE_TYPE_MAP.put("qdf", "AC9EBD8F");  //Quicken (qdf)
+        FILE_TYPE_MAP.put("pwl", "E3828596");  //Windows Password (pwl)
+        FILE_TYPE_MAP.put("wav", "57415645");  //Wave (wav)
+        FILE_TYPE_MAP.put("avi", "41564920");
+        FILE_TYPE_MAP.put("ram", "2E7261FD");  //Real Audio (ram)
+        FILE_TYPE_MAP.put("rm", "2E524D46");  //Real Media (rm)
+        FILE_TYPE_MAP.put("mpg", "000001BA");  //
+        FILE_TYPE_MAP.put("mov", "6D6F6F76");  //Quicktime (mov)
+        FILE_TYPE_MAP.put("asf", "3026B2758E66CF11"); //Windows Media (asf)
+        FILE_TYPE_MAP.put("mid", "4D546864");  //MIDI (mid)
+    }
+
+//    public static void main(String[] args) throws Exception {
+//        File f = new File("/Users/eganwu/Downloads/1645595403457.gif");
+//        if (f.exists()) {
+//            String filetype1 = getImageFileType(f);
+//            System.out.println(filetype1);
+//            String filetype2 = getFileByFile(f);
+//            System.out.println(filetype2);
+//        }
+//    }
+
+    /**
+     * Created on 2010-7-1
+     * <p>Discription:[getImageFileType,获取图片文件实际类型,若不是图片则返回null]</p>
+     *
+     * @param File
+     * @return fileType
+     * @author:[shixing_11@sina.com]
+     */
+    public final static String getImageFileType(File f) {
+        if (isImage(f)) {
+            try {
+                ImageInputStream iis = ImageIO.createImageInputStream(f);
+                Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
+                if (!iter.hasNext()) {
+                    return null;
+                }
+                ImageReader reader = iter.next();
+                iis.close();
+                return reader.getFormatName();
+            } catch (IOException e) {
+                return null;
+            } catch (Exception e) {
+                return null;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Created on 2010-7-1
+     * <p>Discription:[getFileByFile,获取文件类型,包括图片,若格式不是已配置的,则返回null]</p>
+     *
+     * @param file
+     * @return fileType
+     * @author:[shixing_11@sina.com]
+     */
+    public final static String getFileByFile(File file) {
+        String filetype = null;
+        byte[] b = new byte[50];
+        try {
+            InputStream is = new FileInputStream(file);
+            is.read(b);
+            filetype = getFileTypeByStream(b);
+            is.close();
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return filetype;
+    }
+
+    /**
+     * Created on 2010-7-1
+     * <p>Discription:[getFileTypeByStream]</p>
+     *
+     * @param b
+     * @return fileType
+     * @author:[shixing_11@sina.com]
+     */
+    public final static String getFileTypeByStream(byte[] b) {
+        String filetypeHex = String.valueOf(getFileHexString(b));
+        Iterator<Map.Entry<String, String>> entryiterator = FILE_TYPE_MAP.entrySet().iterator();
+        while (entryiterator.hasNext()) {
+            Map.Entry<String, String> entry = entryiterator.next();
+            String fileTypeHexValue = entry.getValue();
+            if (filetypeHex.toUpperCase().startsWith(fileTypeHexValue)) {
+                return entry.getKey();
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Created on 2010-7-2
+     * <p>Discription:[isImage,判断文件是否为图片]</p>
+     *
+     * @param file
+     * @return true 是 | false 否
+     * @author:[shixing_11@sina.com]
+     */
+    public static final boolean isImage(File file) {
+        boolean flag = false;
+        try {
+            BufferedImage bufreader = ImageIO.read(file);
+            int width = bufreader.getWidth();
+            int height = bufreader.getHeight();
+            if (width == 0 || height == 0) {
+                flag = false;
+            } else {
+                flag = true;
+            }
+        } catch (IOException e) {
+            flag = false;
+        } catch (Exception e) {
+            flag = false;
+        }
+        return flag;
+    }
+
+    /**
+     * Created on 2010-7-1
+     * <p>Discription:[getFileHexString]</p>
+     *
+     * @param b
+     * @return fileTypeHex
+     * @author:[shixing_11@sina.com]
+     */
+    public final static String getFileHexString(byte[] b) {
+        StringBuilder stringBuilder = new StringBuilder();
+        if (b == null || b.length <= 0) {
+            return null;
+        }
+        for (int i = 0; i < b.length; i++) {
+            int v = b[i] & 0xFF;
+            String hv = Integer.toHexString(v);
+            if (hv.length() < 2) {
+                stringBuilder.append(0);
+            }
+            stringBuilder.append(hv);
+        }
+        return stringBuilder.toString();
+    }
+}