文件上传

This commit is contained in:
wangkang
2017-07-29 10:18:35 +08:00
parent 7d0ffa183c
commit e61f615453
4 changed files with 231 additions and 7 deletions
@@ -0,0 +1,148 @@
/**
* FileInfo.java
* Created at 2017-05-27
* Created by Administrator
* Copyright (C) 2016 egridcloud.com, All rights reserved.
*/
package com.egridcloud.udf.file.domain;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* 描述 : FileInfo
*
* @author Administrator
*
*/
@ApiModel(description = "文件信息")
public class FileInfo implements Serializable {
/**
* 描述 : ID
*/
private static final long serialVersionUID = 1L;
/**
* 描述 : 文件ID
*/
@ApiModelProperty(value = "文件ID(相对路径的base64编码)", required = true, dataType = "string")
private String id;
/**
* 描述 : 文件名称
*/
@ApiModelProperty(value = "文件名称", required = true, dataType = "string")
private String name;
/**
* 描述 : 文件相对路径
*/
@ApiModelProperty(value = "文件相对路径", required = true, dataType = "string")
private String relativePath;
/**
* 描述 : 上下文类型
*/
@ApiModelProperty(value = "上下文类型", required = true, dataType = "string")
private String contentType;
/**
* 描述 : 文件长度
*/
@ApiModelProperty(value = "文件长度", required = true, dataType = "long")
private Long size;
/**
* 描述 : 获取size
*
* @return the size
*/
public Long getSize() {
return size;
}
/**
* 描述 : 设置size
*
* @param size the size to set
*/
public void setSize(Long size) {
this.size = size;
}
/**
* 描述 : 获取contentType
*
* @return the contentType
*/
public String getContentType() {
return contentType;
}
/**
* 描述 : 设置contentType
*
* @param contentType the contentType to set
*/
public void setContentType(String contentType) {
this.contentType = contentType;
}
/**
* 描述 : 获取relativePath
*
* @return the relativePath
*/
public String getRelativePath() {
return relativePath;
}
/**
* 描述 : 设置relativePath
*
* @param relativePath the relativePath to set
*/
public void setRelativePath(String relativePath) {
this.relativePath = relativePath;
}
/**
* 描述 : 获取name
*
* @return the name
*/
public String getName() {
return name;
}
/**
* 描述 : 设置name
*
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* 描述 : 获取id
*
* @return the id
*/
public String getId() {
return id;
}
/**
* 描述 : 设置id
*
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
}
@@ -6,12 +6,20 @@
*/
package com.egridcloud.udf.file.service;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.egridcloud.udf.core.ApplicationConfig;
import com.egridcloud.udf.file.FileException;
import com.egridcloud.udf.file.FileProperties;
import com.egridcloud.udf.file.domain.FileInfo;
/**
* 描述 : FileService
@@ -28,6 +36,67 @@ public class FileService {
@Autowired
private FileProperties fileProperties;
/**
* 描述 : applicationConfig
*/
@Autowired
private ApplicationConfig applicationConfig;
/**
* 描述 : 上传文件
*
* @param pathCode
* @param file
* @return
* @throws IOException
* @throws IllegalStateException
*/
public FileInfo upload(String pathCode, MultipartFile file)
throws IllegalStateException, IOException {
//验证
this.verification(pathCode, file);
//创建返回对象
FileInfo fileInfo = new FileInfo();
//获得rootPath
String rootPath = fileProperties.getRootPath();
//根据pathCode获得path
String path = fileProperties.getPath().get(pathCode).getPath();
//获得当前时间戳
String day = new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/";
//获得文件名
String originalFilename = file.getOriginalFilename();
fileInfo.setName(originalFilename);
//获得上下文类型
String contentType = file.getContentType();
fileInfo.setContentType(contentType);
//获得文件长度
Long size = file.getSize();
fileInfo.setSize(size);
//拼接相对路径(目录)
String relativePath = path + day;
//拼接相对路径(文件)
String relativeFilePath = relativePath + originalFilename;
fileInfo.setRelativePath(relativeFilePath);
//拼接绝对路径(目录)
String absolutePath = rootPath + relativePath;
//拼接绝对路径(文件)
String absoluteFilePath = rootPath + relativeFilePath;
//获得base64文件ID(通过文件相对路径计算获得)
String fileId = new String(
Base64.encodeBase64(fileInfo.getRelativePath().getBytes(applicationConfig.getEncoding())));
fileInfo.setId(fileId);
//获得文件对象(目录)
File dest = new File(absolutePath);
//创建目录
dest.mkdirs();
//获得文件对象
dest = new File(absoluteFilePath);
//保存
file.transferTo(dest);
//返回
return fileInfo;
}
/**
* 描述 : 验证
*
@@ -6,11 +6,14 @@
*/
package com.egridcloud.udf.file.web;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.egridcloud.udf.core.RestResponse;
import com.egridcloud.udf.file.domain.FileInfo;
import com.egridcloud.udf.file.service.FileService;
/**
@@ -29,8 +32,9 @@ public class FileController implements IFileController {
private FileService fileService;
@Override
public RestResponse<String> upload(String pathCode, Boolean cover, MultipartFile file) {
return new RestResponse<>(pathCode);
public RestResponse<FileInfo> upload(String pathCode, MultipartFile file)
throws IllegalStateException, IOException {
return new RestResponse<>(fileService.upload(pathCode, file));
}
}
@@ -6,11 +6,14 @@
*/
package com.egridcloud.udf.file.web;
import java.io.IOException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import com.egridcloud.udf.core.RestResponse;
import com.egridcloud.udf.file.domain.FileInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
@@ -32,9 +35,10 @@ public interface IFileController {
* 描述 : 上传文件
*
* @param pathCode 路径代码
* @param cover 是否覆盖
* @param file 文件
* @return 结果
* @return
* @throws IllegalStateException 异常
* @throws IOException 异常
*/
@ApiOperation(value = "FILE_1", notes = "上传文件")
@ApiImplicitParams({
@@ -46,11 +50,10 @@ public interface IFileController {
required = true, dataType = "string"),
@ApiImplicitParam(paramType = "form", name = "pathCode", value = "路径代码", required = true,
dataType = "string"),
@ApiImplicitParam(paramType = "form", name = "cover", value = "是否覆盖", required = true,
dataType = "boolean"),
@ApiImplicitParam(paramType = "form", name = "file", value = "文件", required = true,
dataType = "__file") })
@RequestMapping(value = "upload", method = RequestMethod.POST)
public RestResponse<String> upload(String pathCode, Boolean cover, MultipartFile file);
public RestResponse<FileInfo> upload(String pathCode, MultipartFile file)
throws IllegalStateException, IOException;
}