package com.cloud.common.utils; import com.cloud.common.model.FileInfos; import com.github.tobato.fastdfs.domain.fdfs.StorePath; import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray; import com.github.tobato.fastdfs.service.FastFileStorageClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.Date; import java.util.UUID; @Configuration public class FastDFSUtil { @Autowired private FastFileStorageClient fastFileStorageClient; public FileInfos upload(MultipartFile file) { //上传文件 try { String ext_Name = file.getOriginalFilename().split("\\.")[1]; byte[] bytes = null; try { bytes = file.getBytes(); } catch (IOException e) { e.printStackTrace(); } //获取文件大小 long fileSize = file.getSize(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); StorePath storePath = fastFileStorageClient.uploadFile(byteArrayInputStream, fileSize, ext_Name, null); // String fileIds[] = storageClient.upload_file(bytes, ext_Name, null); //获取文件信息,并保存至数据库 FileInfos fileInfo = new FileInfos(); fileInfo.setId(UUID.randomUUID().toString()); fileInfo.setName(file.getOriginalFilename()); fileInfo.setExtName(ext_Name); fileInfo.setPath(storePath.getFullPath()); fileInfo.setSize(file.getSize()); fileInfo.setCreateTime(new Date()); return fileInfo; } catch (Exception e) { e.printStackTrace(); return null; } } public FileInfos uploadByByte(byte[] bytes, String extName) { //上传文件 try { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); StorePath storePath = fastFileStorageClient.uploadFile(byteArrayInputStream, bytes.length, extName, null); // String fileIds[] = storageClient.upload_file(bytes, extName, null); //获取文件信息,并保存至数据库 FileInfos fileInfo = new FileInfos(); fileInfo.setExtName(extName); fileInfo.setPath(storePath.getFullPath()); fileInfo.setCreateTime(new Date()); return fileInfo; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 根据图片的FastDFS地址获得数据的字节流 * @param strUrl 网络连接地址 * @return */ public byte[] getImageFromFDFS(String strUrl){ try { URL url = new URL(strUrl); System.out.println("url是:"+strUrl); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); InputStream inStream = conn.getInputStream();//获取从网络的输入流 byte[] btImg = new byte[inStream.available()]; inStream.read(btImg); //从输入流中得到图片的二进制数据 return btImg; } catch (Exception e) { e.printStackTrace(); } return null; } public byte[] downloadFDFS(String fileUrl) throws Exception { String group = fileUrl.substring(0, fileUrl.indexOf("/")); String path = fileUrl.substring(fileUrl.indexOf("/") + 1); DownloadByteArray downloadByteArray = new DownloadByteArray(); return fastFileStorageClient.downloadFile(group, path, downloadByteArray); } }