Demostración de introducción a la carga de imágenes

Primero necesitamos instalar un  servidor de archivos distribuido (por ejemplo, FastDFS) y luego obtener su dirección (por ejemplo, 192.168.25.133:22122 )

 

1. Construcción del proyecto, importación de coordenadas dependientes

<!-- 分布式文件服务器依赖 -->
<dependency>
    <groupId>org.csource.fastdfs</groupId>
    <artifactId>fastdfs</artifactId>
    <version>1.2</version>
</dependency>

2. Cree un archivo de configuración

Cree el archivo de configuración fdfs_client.conf en recursos:

# connect timeout in seconds
# default value is 30s
connect_timeout=30

# network timeout in seconds
# default value is 30s
network_timeout=60

# the base path to store log files
base_path=/home/fastdfs

# tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
tracker_server=192.168.25.133:22122

#standard log level as syslog, case insensitive, value list:
### emerg for emergency
### alert
### crit for critical
### error
### warn for warning
### notice
### info
### debug
log_level=info

# if use connection pool
# default value is false
# since V4.05
use_connection_pool = false

# connections whose the idle time exceeds this time will be closed
# unit: second
# default value is 3600
# since V4.05
connection_pool_max_idle_time = 3600

# if load FastDFS parameters from tracker server
# since V4.05
# default value is false
load_fdfs_parameters_from_tracker=false

# if use storage ID instead of IP address
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# default value is false
# since V4.05
use_storage_id = false

# specify storage ids filename, can use relative or absolute path
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# since V4.05
storage_ids_filename = storage_ids.conf


#HTTP settings
http.tracker_server_port=80

#use "#include" directive to include HTTP other settiongs
##include http.conf

*** La dirección de tracker_server en el archivo es la dirección de instalación del servidor de archivos distribuidos

3. Prueba

Crea una clase de prueba:

// 1、加载 fdfs_client.conf 配置文件地址
ClientGlobal.init("D:\\MyProject\\Idea\\fastdfs_demo\\src\\main\\resources\\fdfs_client.conf");
// 2、创建一个 TrackerClient 对象。直接 new 一个
TrackerClient trackerClient = new TrackerClient();
// 3、使用 TrackerClient 对象创建连接,获得一个 TrackerServer 对象。
TrackerServer trackerServer = trackerClient.getConnection();
// 4、创建一个 StorageServer 的引用,值为 null
StorageServer storageServer = null;
// 5、创建一个 StorageClient 对象,需要两个参数 TrackerServer 对象、StorageServer 的引用
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
// 6、使用 StorageClient 对象上传图片
//扩展名不带“.”
String[] strings = storageClient.upload_file("C:\\Users\\Zenith\\Desktop\\0001.jpg", "jpg", null);
// 7、返回数组。包含组名和图片的路径。
for (String string : strings) {
    System.out.println(string);
}

La salida de impresión de prueba es una matriz:

group1
M00/00/00/wKgZhVwLL32ASCEeAABirWQnsTw252.jpg

Ingrese la dirección en el navegador para ver las imágenes que se han subido al  servidor de archivos distribuidos:

http://192.168.25.133/group1/M00/00/00/wKgZhVwLL32ASCEeAABirWQnsTw252.jpg

 

 

Descarga del código fuente:  https://pan.baidu.com/s/1tQGll4_Up1CKNA-O-KPLGw

Supongo que te gusta

Origin blog.csdn.net/weixin_42629433/article/details/84890412
Recomendado
Clasificación