1、controller:
记得加上两个注解:@CrossOrigin // 注解方式@RestController
//注入fastDFSClient,关于fastDFSClient的初始化,在本专栏的另一篇中写了
@Autowired
FastDFSClient fastDFSClient;
/**
* 常用接口,dfs文件上传(文件标签和isAuth为必填项;isAuth为文件是否有效标识,无效文件定期清除,有效:0,无效:1;为空时默认为0有效)
* @param file
* @param dto
* @return ResultDto
*/
@ApiOperation(value="常用接口,dfs文件上传(文件标签和isAuth为必填项;isAuth为文件是否有效标识,无效文件定期清除,有效:0,无效:1;为空时默认为0有效)", notes="null")
@PostMapping(value = "/dfsupload")
public ItsSingleResultDto dfsUpload(
@ApiParam(value = "", required = true) @RequestParam( value="file", required = true) MultipartFile file,
@ApiParam(value = "文件信息", required = true) @ModelAttribute FileQueryDto dto
) {
ItsSingleResultDto<FileDto> result = new ItsSingleResultDto<FileDto>();//自己定义的返回值对象
FileDetailDto newdto = new FileDetailDto();
String staffCode = this.getStaffCode();//上传文件人的卡号
String name = file.getOriginalFilename();
String suffix = name.substring(name.lastIndexOf(".") + 1, name.length());
// FileDetailDto backpatha=new FileDetailDto();
UploadDto uploadDto = new UploadDto();
java.io.File f = new java.io.File(name);
byte[] buffer = null;
try {
buffer = file.getBytes();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
uploadDto.setFile(buffer);
uploadDto.setFileName(name);
// backpatha=service.dfsupload(uploadDto);//上传改到服务端实现
Map<String, String> metaList = new HashMap<String, String>();
String backpath = fastDFSClient.uploadFile(uploadDto.getFile(), uploadDto.getFileName(), metaList);//进行上传
// backpath = null;
//做循环,如果上传失败了,则循环再次尝试上传
while (StringUtils.isEmpty(backpath)) {
backpath = fastDFSClient.uploadFile(uploadDto.getFile(), uploadDto.getFileName(), metaList);
System.out.println("00000+" + backpath);
}
System.out.println(backpath);
//-----------------------文件上传完毕,根据自己的业务逻辑,向数据库表中插入记录。下方全部是业务处理了。--------------------
BeanUtils.copyProperties(dto, newdto);//属性对拷
// newdto.setPath(fileName);
newdto.setFilename(name);
newdto.setSize(file.getSize());
newdto.setType(suffix);
newdto.setFakepath(backpath);
newdto.setUploaderno(staffCode);
FileDto filedto = new FileDto();
BeanUtils.copyProperties(newdto, filedto);
// 插入主表
filedto.setDownloadcount(0);
// 当前系统时间
// Date day=new Date();
Date day = com.dhc.its.utils.GlobalHelper.getDateTime();
// System.out.println("11111111111+"+day);
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
filedto.setUploadtime(df.format(day));
if (StringUtils.isEmpty(dto.getIsAuth()) || dto.getIsAuth().equals("")) {
filedto.setIsauth(Constant.NOT_DELETE);
} else {
filedto.setIsauth(dto.getIsAuth());
}
// filedto.setUploadtime(day);
FileDto res = BeanMapper.map(fileAS.save(BeanMapper.map(filedto, File.class)), FileDto.class);
dto.setFileuid(res.getFileuid());
res.setFakepath(null);// 屏蔽fakepath
// FileDetailDto result=BeanMapper.map(file, FileDetailDto.class);
// 循环插入标签表
FileTagDto fileTagDto = new FileTagDto();
if (StringUtils.isEmpty(dto.getFileuid()) && StringUtils.isEmpty(dto.getTagid())) {
} else {
String tagarr[] = dto.getTagid().split(",");
for (int i = 0; i < tagarr.length; i++) {
dto.setTagid(tagarr[i]);
BeanUtils.copyProperties(dto, fileTagDto);
Boolean fileTag = fileTagAS.create(BeanMapper.map(fileTagDto, FileTag.class));
result.setCode(ResultCodeEnum.SUCCESS.getCode());
result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
result.setData(res);
return result;
}
2、在FastDFSClient中加入上传方法
/**
* 上传文件
*
* @param file
* 文件对象
* @param fileName
* 文件名
* @param metaList
* 文件元数据
* @return
*/
@Autowired
public StorageClient1 storageClient1;//这个需要在上方初始化,请看我构建FastDFS的client的博文。
public String uploadFile(byte[] buff, String fileName, Map<String, String> metaList) {
try {
// byte[] buff = IOUtils.toByteArray(new FileInputStream(file));
NameValuePair[] nameValuePairs = null;
if (metaList != null) {
nameValuePairs = new NameValuePair[metaList.size()];
int index = 0;
for (Iterator<Map.Entry<String, String>> iterator = metaList.entrySet().iterator(); iterator
.hasNext();) {
Map.Entry<String, String> entry = iterator.next();
String name = entry.getKey();
String value = entry.getValue();
nameValuePairs[index++] = new NameValuePair(name, value);
}
}
return storageClient1.upload_file1(buff, FilenameUtils.getExtension(fileName), nameValuePairs);
// FileUtils.getExtension(fileName)
} catch (Exception e) {
e.printStackTrace();
return null;
}
// return storageClient1.upload_file1(buff,fileName,nameValuePairs);
}
3、可能涉及到的相关pom依赖:
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>1.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.13</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.3</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.2.2</version>
</dependency>