You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.5 KiB
71 lines
2.5 KiB
5 years ago
|
package com.yipin.liuwanr;
|
||
|
|
||
|
import com.yipin.liuwanr.service.AliyunOssService;
|
||
|
import org.apache.commons.fileupload.FileItem;
|
||
|
import org.apache.commons.fileupload.disk.DiskFileItem;
|
||
|
import org.apache.commons.io.IOUtils;
|
||
|
import org.junit.Before;
|
||
|
import org.junit.Test;
|
||
|
import org.junit.runner.RunWith;
|
||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||
|
import org.springframework.mock.web.MockHttpServletResponse;
|
||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||
|
import org.springframework.test.web.servlet.MockMvc;
|
||
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||
|
import org.springframework.web.context.WebApplicationContext;
|
||
|
import org.springframework.web.multipart.MultipartFile;
|
||
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
||
|
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.io.File;
|
||
|
import java.io.FileInputStream;
|
||
|
import java.io.IOException;
|
||
|
import java.nio.file.Files;
|
||
|
import java.util.HashMap;
|
||
|
|
||
|
@RunWith(SpringRunner.class)
|
||
|
@SpringBootTest
|
||
|
public class AliyunOssServiceTest {
|
||
|
|
||
|
@Autowired
|
||
|
private AliyunOssService aliyunOssService;
|
||
|
|
||
|
/**
|
||
|
* 测试上传文件
|
||
|
*/
|
||
|
@Test
|
||
|
public void testUploadFiles() throws IOException {
|
||
|
File file = new File("D:\\pic\\1.jpg");
|
||
|
FileItem fileItem = new DiskFileItem("file",
|
||
|
Files.probeContentType(file.toPath()),
|
||
|
false,
|
||
|
file.getName(),
|
||
|
(int) file.length(),
|
||
|
file.getParentFile());
|
||
|
IOUtils.copy(new FileInputStream(file), fileItem.getOutputStream());
|
||
|
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
|
||
|
HashMap<String, Object> map = aliyunOssService.uploadFiles(multipartFile);
|
||
|
map.forEach((key, value) -> System.out.println("key = " + key + " ===> value = " + value.toString()));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 测试下载文件
|
||
|
*/
|
||
|
@Test
|
||
|
public void testDownloadFiles() throws IOException {
|
||
|
HttpServletResponse response = new MockHttpServletResponse();
|
||
|
AliyunOssService aliyunOssService = this.aliyunOssService;
|
||
|
aliyunOssService.downloadFiles(response,
|
||
|
"http://liuwanr.oss-cn-shenzhen.aliyuncs.com/jpg/20200807/1596787474773.jpg");
|
||
|
}
|
||
|
/**
|
||
|
* 测试根据文件路径+文件名称,删除该文件
|
||
|
*/
|
||
|
@Test
|
||
|
public void testDeleteFile() throws IOException {
|
||
|
aliyunOssService.deleteFile("http://liuwanr.oss-cn-shenzhen.aliyuncs.com/jpg/20200807/1596787474773.jpg");
|
||
|
}
|
||
|
|
||
|
}
|