Use fastDFS upload and download image files

package com.xuecheng.test.fastdfs;

import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* @author newcityman
* @date 2020/2/16 - 15:01
*/
@SpringBootTest
@RunWith (. SpringRunner class)
public class TestFastDFS {
// upload @Test public void the testUpload () { the try { // load fastdfs-client.properties profile ClientGlobal. InitByProperties ( "config / fastdfs_client.properties") ; // defined TrackerClient, for requesting Tracker TrackerClient trackerClient = new new TrackerClient () ; // connected Tracker trackerServer trackerServer trackerClient.getConnection = () ; // Get Storage server storageServer storageserver = trackerClient.getStoreStorage (trackerServer) ; // Create a StorageClient StorageClient1 storageClient1 = new new StorageClient1 (trackerServer












, storageserver) ; // upload files to a storage server String filePath = "H: /wx_cz.jpg" ; String fileId = storageClient1.upload_file1 (filePath , "JPG" , null) ; System. out.println (fileId) ; // named group1 / M00 / 00/00 / wKgAaV5JY7CAGDfKAAAjb9XlGuI005.jpg } the catch (IOException E) { e.printStackTrace () ; } the catch (MyException E) { e.printStackTrace () ; } } // download file @Test public void testDownload () { the try { // load fastdfs-client.properties profile ClientGlobal. initByProperties (

















"config / fastdfs_client.properties") ; // define TrackerClient, for requesting Tracker TrackerClient trackerClient = new new TrackerClient () ; // connected Tracker TrackerServer trackerServer trackerClient.getConnection = () ; // Get Storage Server StorageServer storageServer = trackerClient.getStoreStorage (trackerServer) ; // create a StorageClient StorageClient1 storageClient1 = new new StorageClient1 (trackerServer , storageserver) ; // download file // file the above mentioned id String fileId = "group1 / M00 / 00/00 / wKgAaV5JY7CAGDfKAAAjb9XlGuI005.jpg" ; byte [] bytes = storageClient1.download_file1 (fileId) ; // save the file output stream using













FileOutputStream fileOutputStream = new FileOutputStream(new File("i:/lg.jpg"));
fileOutputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}

}

}

Guess you like

Origin www.cnblogs.com/newcityboy/p/12319715.html
Recommended