a
554325746@qq.com
2019-12-25 603cb36a5123e46656b06a5deb8d7ac7ff81307f
app/src/main/java/com/basic/security/utils/FastDFSUtil.java
@@ -43,8 +43,8 @@
            byte[] by = getBytes(fdfs_client_path);
            String fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length());
            String resultPath = uploadFile(by, fileName, type);
//            System.out.println(TAG + "上传后相对存储路径:" + resultPath);
//             System.out.println("访问路径:"+PropertiesUtils.getInfo("fastdfs_url")+resultPath);
//            System1.out.println(TAG + "上传后相对存储路径:" + resultPath);
//             System1.out.println("访问路径:"+PropertiesUtils.getInfo("fastdfs_url")+resultPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
@@ -58,7 +58,7 @@
            return uploadFile(data, fileName, "jpg");
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("FastDFSUtil.upLoadVisitorPic " + e.getMessage());
            System1.out.println("FastDFSUtil.upLoadVisitorPic " + e.getMessage());
            return null;
        }
    }
@@ -97,43 +97,43 @@
        String path;
        TrackerServer trackerServer = null;
        try {
//            System.out.println(TAG + "上传文件名:" + name);
//            System1.out.println(TAG + "上传文件名:" + name);
//         if (!getInfo("file_type_list").contains(ext)
//               && !getInfo("file_type_list").toUpperCase().contains(ext)) {
//            System.out.println("不支持" + ext + "类型文件上传!");
//            System1.out.println("不支持" + ext + "类型文件上传!");
//            throw new RuntimeException("不支持" + ext + "类型文件上传!");
//         }
//            System.out.println(TAG + "上传文件类型: " + ext);
//            System1.out.println(TAG + "上传文件类型: " + ext);
            if (pic.length / 1024 / 1024 > 10) {
//                System.out.println(TAG + "仅支持10MB以下文件上传!");
//                System1.out.println(TAG + "仅支持10MB以下文件上传!");
                throw new RuntimeException(TAG + "仅支持10MB以下文件上传!");
            }
//            System.out.println(TAG + "文件大小:" + pic.length / 1024 + "KB");
//            System1.out.println(TAG + "文件大小:" + pic.length / 1024 + "KB");
            String fdfs_client_path = new ContextWrapper(BaseApplication.getApplication().activity).getFilesDir().getAbsolutePath();
            ClientGlobal.init(fdfs_client_path + "/fdfs_client.properties");
            TrackerClient trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
//            System.out.println(TAG + "tracker服务器连接成功! " + trackerServer.getInetSocketAddress());
//            System1.out.println(TAG + "tracker服务器连接成功! " + trackerServer.getInetSocketAddress());
            StorageServer storageServer = null;
            StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
//            System.out.println(TAG + "启动FastDFS客户端.... :" + storageClient1);
//            System1.out.println(TAG + "启动FastDFS客户端.... :" + storageClient1);
            NameValuePair[] meta_list = new NameValuePair[3];
            meta_list[0] = new NameValuePair("filename", name);
            meta_list[1] = new NameValuePair("fileext", ext);
            meta_list[2] = new NameValuePair("filesize", String.valueOf(pic.length));
            path = storageClient1.upload_file1(pic, ext, meta_list);
            if (null != path && !"".equals(path)) {
//                System.out.println(TAG + "文件上传成功!   " + path);
//                System1.out.println(TAG + "文件上传成功!   " + path);
                return path;
            } else {
//                System.out.println(TAG + "文件上传失败!");
//                System1.out.println(TAG + "文件上传失败!");
                throw new RuntimeException(TAG + "文件上传失败!");
            }
        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println(TAG + "系统异常,文件上传失败!");
//            System1.out.println(TAG + "系统异常,文件上传失败!");
//            throw new RuntimeException(TAG + "系统异常,文件上传失败!" + e.getMessage());
            System.out.println("FastDFSUtil.uploadFile " + e.getMessage());
            System1.out.println("FastDFSUtil.uploadFile " + e.getMessage());
        } finally {
            if (trackerServer != null) {
                try {
@@ -156,7 +156,7 @@
    @SuppressWarnings("unused")
    public static String downFile(String fileUrl) {
        if (null == fileUrl || "".equals(fileUrl)) {
//            System.out.println("下载文件源路径为空!");
//            System1.out.println("下载文件源路径为空!");
            return null;
        }
        BufferedInputStream dis = null;
@@ -166,14 +166,14 @@
            // 存储下载文件的路径,默认存储在当前用户所在目录下
            filePath = System.getProperty("user.home") + File.separator
                    + fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
//            System.out.println("本地存储路径:" + filePath);
//            System1.out.println("本地存储路径:" + filePath);
            File destFile = new File(filePath);
            if (!destFile.exists()) {
                destFile.createNewFile();
            }
            URL url = new URL(getInfo("http_head") + getInfo("fastdfs_vip")
                    + fileUrl);
//            System.out.println("文件源路径:" + url);
//            System1.out.println("文件源路径:" + url);
            dis = new BufferedInputStream(url.openStream());
            fos = new BufferedOutputStream(new FileOutputStream(destFile));
            byte[] buff = new byte[2048];
@@ -181,9 +181,9 @@
            while (-1 != (bytesRead = dis.read(buff, 0, buff.length))) {
                fos.write(buff, 0, bytesRead);
            }
//            System.out.println("文件下载成功!");
//            System1.out.println("文件下载成功!");
        } catch (Exception e) {
//            System.out.println("文件下载出现未知异常!");
//            System1.out.println("文件下载出现未知异常!");
            return null;
        } finally {
            if (dis != null) {