1.通过上传文件到FastDF,要获取file属性
public String SeCondPublish(GoodsModel goodsModel, HttpServletRequest request, @RequestParam MultipartFile[] filedata) {
MultipartFile file = filedata[0];
CommonsMultipartFile cf= (CommonsMultipartFile)file;
DiskFileItem fi = (DiskFileItem)cf.getFileItem();
File f = fi.getStoreLocation();
FileSystemServiceImpl impl = new FileSystemServiceImpl();
String filePath = impl.uploadFile("jpg", impl.getBytesFromFile(f));
//上传到服务器的FASTDFS配置工具类
FileSystemServiceImpl.java
FileSystemServiceImpl implements IFileSystemService
/**
* 上次文件到服务器
* @throws Exception
*/
@SuppressWarnings("deprecation")
public String uploadFile(String fileExtName, byte[] datas){
String result = null;
try{
URL url = this.getClass().getClassLoader().getResource("/");
if(url==null){
url = this.getClass().getClassLoader().getResource("");
}
ClientGlobal.init(java.net.URLDecoder.decode(url.getPath())+java.io.File.separator+"fdfs_client.conf");
//ClientGlobal.init(this.getClass().getClassLoader().getResource("/").getPath()+"fdfs_client.conf");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String group_name = null;
long startTime = System.currentTimeMillis();
String[] results = storageClient.upload_file(datas, fileExtName, null);
System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (results != null){
group_name = results[0];
String remote_filename = results[1];
System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
System.err.println(storageClient.get_file_info(group_name, remote_filename));
result = remote_filename;
ServerInfo[] servers = trackerClient.getFetchStorages(trackerServer, group_name, remote_filename);
if (servers == null){
System.err.println("get storage servers fail, error code: " + trackerClient.getErrorCode());
} else {
System.err.println("storage servers count: " + servers.length);
for (int k = 0; k < servers.length; k++){
System.err.println(k + 1 + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
}
System.err.println("");
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
return result;
}
}
/**
* 下载文件
*
* @throws Exception
*/
@SuppressWarnings("finally")
public byte[] downFile(String remote_filename) {
byte[] fileByte = null;
try {
URL url = this.getClass().getClassLoader().getResource("/");
if (url == null) {
url = this.getClass().getClassLoader().getResource("");
}
ClientGlobal.init(java.net.URLDecoder.decode(url.getPath())
+ java.io.File.separator + "fdfs_client.conf");
// ClientGlobal.init(this.getClass().getClassLoader().getResource("/").getPath()+"\\fdfs_client.conf");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
FileInfo fi = storageClient.get_file_info("group1", remote_filename);
String sourceIpAddr = fi.getSourceIpAddr();
long size = fi.getFileSize();
fileByte = storageClient.download_file("group1", remote_filename);
System.out.println("ip:" + sourceIpAddr + ",size:" + size + " downFile:" + fileByte.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
return fileByte;
}
}
/**
* 删除文件
* @param fileId 文件id
* @throws Exception
*/
@SuppressWarnings("finally")
public int delFile(String remote_filename) {
int returnVal = -1;// 0文件存在下的删除,2文件不存在删除
try {
URL url = this.getClass().getClassLoader().getResource("/");
if (url == null) {
url = this.getClass().getClassLoader().getResource("");
}
ClientGlobal.init(java.net.URLDecoder.decode(url.getPath()) + java.io.File.separator + "fdfs_client.conf");
// ClientGlobal.init(this.getClass().getClassLoader().getResource("/").getPath()+"\\fdfs_client.conf");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
returnVal = storageClient.delete_file("group1", remote_filename);
} catch (Exception e) {
e.printStackTrace();
} finally {
return returnVal;
}
}
public static void main(String[] args) {
FileSystemServiceImpl impl = new FileSystemServiceImpl();
File file = new File("C:\\Users\\Administrator\\Desktop\\56181431496178891.jpg");
String filePath = impl.uploadFile("jpg", getBytesFromFile(file));
System.out.println("filePath---------------------->" + filePath);
impl.delFile("M00/00/00/wKgB5lYndwWAd380AAGD9FDKp8w9.t.jpg");
}
public static byte[] getBytesFromFile(File f) {
if (f == null) {
return null;
}
try {
FileInputStream stream = new FileInputStream(f);
ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
for (int n; (n = stream.read(b)) != -1;) {
out.write(b, 0, n);
}
stream.close();
out.close();
return out.toByteArray();
} catch (IOException e) {
}
return null;
}