简介
sharp-fileupload
可以上传文件,对图片的处理(裁剪、选装、缩放)。提供了Restfull接口上传,访问文档。底层存储功能支持:
- 本地存储
- 阿里云OSS
- FastDFS
添加依赖
<dependency>
<groupId>com.rick.fileupload</groupId>
<artifactId>sharp-fileupload</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
配置存储策略
本地存储
默认就是本地存储。
application.xml
fileupload:
tmp: /Users/rick/jkxyx205/tmp/fastdfs/tmp # 下载的临时目录
local:
server-url: http://localhost:7892/ # 映射到tmp目录
root-path: /Users/rick/jkxyx205/tmp/fileupload
文件存储的路径是 root-path
。在本地开启一个静态服务器指向存储地址。访问url是 server-url
。
OSS
创建 Bucket
name为「sharp-fileupload」
application.xml
fileupload:
tmp: /Users/rick/jkxyx205/tmp/fastdfs/tmp # 下载的临时目录
oss:
endpoint: oss-cn-beijing.aliyuncs.com
accessKeyId: xxx
accessKeySecret: xxx
bucketName: sharp-fileupload
添加依赖
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
配置类
@Bean
public InputStreamStore ossInputStreamStore(OSSProperties ossProperties) {
OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());
return new OSSInputStreamStore(ossClient, ossProperties);
}
FastDFS
fdfs_client.properties
fastdfs.tracker_servers=192.168.0.117:22122
fastdfs.http_tracker_http_port=8080
添加依赖
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
配置类
@Bean
public InputStreamStore fastDFSInputStreamStore() throws IOException, MyException {
return new FastDFSInputStreamStore("fdfs_client.properties");
}
测试InputStreamStore
接口信息:
public interface InputStreamStore {
StoreResponse store(String groupName, String extension, InputStream is) throws IOException;
/**
*
* @param groupName
* @param storeName 磁盘存储的文件名
* @param extension 扩展名
* @param is
* @return
* @throws IOException
*/
StoreResponse store(String groupName, String storeName, String extension, InputStream is) throws IOException;
/**
* 删除文件
* @param groupName
* @param path
* @return
*/
void delete(String groupName, String path) throws IOException;
/**
* 获取访问地址
* @param groupName
* @param path
* @return
*/
String getURL(String groupName, String path);
/**
* 获取文件流
* @param groupName
* @param path
* @return
* @throws IOException
*/
InputStream getInputStream(String groupName, String path) throws IOException;
/**
* 获取字节数据
* @param groupName
* @param path
* @return
* @throws IOException
*/
byte[] getByteArray(String groupName, String path) throws IOException;
}
测试代码:
@Autowired
private InputStreamStore inputStreamStore;
private static String path;
@Test
@Order(1)
public void testPropertyStore() throws IOException {
StoreResponse response = inputStreamStore.store("group", "jpeg",
new FileInputStream("/Users/rick/jkxyx205/tmp/fileupload/demo/1.jpg"));
System.out.println(response.getGroupName());
System.out.println(response.getPath());
System.out.println(response.getFullPath());
System.out.println(response.getUrl());
path = response.getPath();
}
@Test
@Order(2)
public void getURL() {
String url = inputStreamStore.getURL("group", path);
System.out.println(url);
}
@Test
@Order(3)
public void getInputStream() throws IOException {
InputStream is = inputStreamStore.getInputStream("group", path);
FileUtils.copyInputStreamToFile(is, new File("/Users/rick/jkxyx205/tmp/fileupload/download/1.png"));
is.close();
}
@Test
@Order(Order.DEFAULT)
public void testPropertyDelete() throws IOException {
inputStreamStore.delete("group", path);
}