package cn.autoform.web.controller.resumable.util; import cn.autoform.util.tool.ObjectUtil; import org.csource.common.MyException; import org.csource.fastdfs.*; import org.springframework.context.EnvironmentAware; import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; import java.io.*; import java.util.Properties; /** * 本地云上传文件 */ @Service public class LocalCloudUploadUtil implements fileStorageConst, EnvironmentAware { private static Environment environment; private static String groupName; public static String getGroupName(){ return groupName; } /** * 取得TRACKER连接 * * @return * @throws MyException * @throws IOException * @throws FileNotFoundException */ public static TrackerServer getTrackerServerInstance() throws IOException, MyException { // 加载配置文件 ClientGlobal.initByProperties(new Properties(){ @Override public String getProperty(String key) { return environment.getProperty(key); } @Override public String getProperty(String key, String defaultValue) { return environment.getProperty(key, defaultValue); } }); TrackerClient track = new TrackerClient(); return track.getConnection(); } /** * 取得CLIENT * * @param tracker * @return */ public static StorageClient1 connect(TrackerServer tracker) { return new StorageClient1(tracker, null); } /** * 上传文件到本地云服务器 * * @return 文件所在组名 文件被编码后的名 */ public static String[] upload(String FilePath, String uploadFileName, TrackerServer trackerServer, StorageClient1 client) { String results[]; try { byte[] atArr = getBytes(FilePath); boolean isAlive = ProtoCommon.activeTest(trackerServer.getSocket()); if (!isAlive) { System.out.println("连接异常"); return null; } synchronized (client) { results = client.upload_file(groupName, atArr, uploadFileName, null); } } catch (Exception e) { return null; } return results; } public static String upload(byte[] in, String uploadFileName) { try { TrackerServer trackerServer = getTrackerServerInstance(); StorageClient1 storageClient1 = connect(trackerServer); String[] res = storageClient1.upload_file(groupName, in, uploadFileName, null); if (ObjectUtil.notEmpty(res)) { return res[0]; } else { return ""; } }catch (IOException|MyException e){ e.printStackTrace(); return ""; } } /** * 关闭 * * @param trackerServer */ public void close(TrackerServer trackerServer) { try { trackerServer.close(); } catch (IOException e) { }finally { try { trackerServer.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 将文件转换成byte数组 * * @return 文件byte数组 * @throws IOException * @throws Exception */ public static byte[] getBytes(String filePath) throws IOException{ byte[] buffer = null; File file = new File(filePath); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); try { byte[] b = new byte[1000]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } buffer = bos.toByteArray(); } catch (FileNotFoundException e) { //e.printStackTrace(); try { throw new FileNotFoundException(); } catch (FileNotFoundException e1) { throw e1; } } catch (IOException e) { throw (e); }finally { if(fis != null){ fis.close(); } if(bos != null){ bos.close(); } } return buffer; } /** * 将byte数组转换成文件 * @throws Exception */ public static void saveFile(byte[] bfile, String filePath, String fileName) { BufferedOutputStream bos = null; FileOutputStream fos = null; File file = null; try { File dir = new File(filePath); if (!dir.exists()) {// 判断文件目录是否存在 dir.mkdirs(); } file = new File(filePath + "\\" + fileName); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(bfile); } catch (Exception e) { //e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e1) { //e1.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e1) { //e1.printStackTrace(); } } } } /** * 下载文件到本地云服务器 */ public static byte[] download(String groupName, String remoteFileName, StorageClient1 client) { byte[] content = null; try { content = client.download_file(groupName, remoteFileName); return content; // System.out.println(content.toString()); } catch (Exception e) { // TODO Auto-generated catch block //e.printStackTrace(); } return null; } @Override public void setEnvironment(Environment environment) { LocalCloudUploadUtil.environment = environment; LocalCloudUploadUtil.groupName = environment.getProperty("fast.group_name"); } }