SpringBoot实现⽂件上传接⼝
摘要
公司都是采⽤SpringBoot作为项⽬框架,其实SpringBoot和SSM框架很接近,基本上只是将SSM的⼀些配置项修改为⾃动配置或者简单的注解配置就可以了,建议不了解的SpringBoot的朋友们可以了解⼀下,上⼿很快,其实⽂件上传框架根本没有多⼤关系。我只是顺便帮SpringBoot打个⼴告罢了。
正题
需求:需要实现⼀个⽂件上传的web接⼝。
1、先实现⼀个Controller接⼝,如下:
package com.lanxuewei.utils.aspect;
import com.lanxuewei.utils.interceptor.annotation.AppIdAuthorization;
import com.del.ReturnCodeAndMsgEnum;
import com.del.ReturnValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
* @author lanxuewei Create in 2018/7/3 20:01
* Description: aop 测试控制器
*/
@RestController
@RequestMapping(value = "/aop")
public class TestController {
private static final Logger logger = Logger(TestController.class);
@Autowired
private TestService testService;
/**
* ⽂件上传测试接⼝
* @return
*/
@AppIdAuthorization
@RequestMapping("/upload")
public ReturnValue uploadFileTest(@RequestParam("uploadFile") MultipartFile zipFile) {
return testService.uploadFileTest(zipFile);
}
}
2、Service接⼝如下:
package com.lanxuewei.utils.aspect;
import org.springframework.web.multipart.MultipartFile;
import com.del.ReturnValue;
public interface TestService {
public ReturnValue uploadFileTest(MultipartFile zipFile);
}
3、Service实现如下:
package com.lanxuewei.utils.aspect;
import com.del.ReturnCodeAndMsgEnum;
import com.del.ReturnValue;
import org.apachemons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;
/**
* @author lanxuewei Create in 2018/8/14 10:01
* Description:
*/
@Service
public class TestServiceImp implements TestService {
private static final Logger logger = Logger(TestServiceImp.class);
@Override
public ReturnValue uploadFileTest(MultipartFile zipFile) {
String targetFilePath = "D:\\test\\uploadTest";
String fileName = UUID.randomUUID().toString().replace("-", "");
File targetFile = new File(targetFilePath + File.separator + fileName);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(targetFile);
logger.info("------>>>>>>uploaded a file successfully!<<<<<<------");
} catch (IOException e) {
return new ReturnValue<>(-1, null);
} finally {
前端大文件上传解决方案try {
fileOutputStream.close();
} catch (IOException e) {
<("", e);
}
}
return new ReturnValue<>(ReturnCodeAndMsgEnum.Success, null);
}
}
说明:
1、targetFilePath为⽂件保存路径,本⼈⽤于测试所以指定路径,可根据实际情况进⾏修改。
2、fileName采⽤UUID⽣成,保证⽂件名唯⼀不重复,但是没有保留原⽂件后缀,可通过获取原⽂件⽂件名后,调⽤lastIndexOf(“.”)获取⽂件原后缀加上。
3、IOUtils为org.apachemons.io.IOUtils,注意别导⼊错误。
4、本⽂中采⽤logback⽇志系统,可根据实际情况修改或删除。
附上ReturnValue以及ReturnCodeAndMsgEnum类,⽤于Controller层统⼀返回前端的model,如下:
package com.del;
import java.io.Serializable;
/**
* @author lanxuewei Create in 2018/7/3 20:05
* Description: 统⼀web返回结果
*/
public class ReturnValue<T> implements Serializable {
private static final long serialVersionUID = -1959544190118740608L;
private int ret;
private String msg;
private T data;
public ReturnValue() {
< = 0;
this.msg = "";
this.data = null;
}
public ReturnValue(int retCode, String msg, T data) {
< = 0;
this.msg = "";
this.data = null;
< = retCode;
this.data = data;
this.msg = msg;
}
public ReturnValue(int retCode, String msg) {
< = 0;
this.msg = "";
this.data = null;
< = retCode;
this.msg = msg;
}
public ReturnValue(ReturnCodeAndMsgEnum codeAndMsg) {
Code(), Msg(), null);
}
public ReturnValue(ReturnCodeAndMsgEnum codeAndMsg, T data) { Code(), Msg(), data);
}
public int getRet() {
;
}
public void setRet(int ret) {
< = ret;
}
public String getMsg() {
return this.msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return this.data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "ReturnValue{" +
"ret=" + ret +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}
package com.del;
/**
* @author lanxuewei Create in 2018/7/3 20:06
* Description: web相关接⼝返回状态枚举
*/
public enum ReturnCodeAndMsgEnum {
Success(0, "ok"),
No_Data(-1, "no data"),
SYSTEM_ERROR(10004, "system error");
private String msg;
private int code;
private ReturnCodeAndMsgEnum(int code, String msg) {
this.msg = msg;
}
public static ReturnCodeAndMsgEnum getByCode(int code) {
ReturnCodeAndMsgEnum[] var1 = values();
int var2 = var1.length;
for(int var3 = 0; var3 < var2; ++var3) {
ReturnCodeAndMsgEnum aiTypeEnum = var1[var3];
if (de == code) {
return aiTypeEnum;
}
}
return Success;
}
public String getMsg() {
return this.msg;
}
public int getCode() {
de;
}
}
Postman发请求返回结果成功,以上代码只需要uploadFile⼀个参数即可。
注意事项: application.properties配置⽂件中可以配置⽂件上传相关属性,配置上传⽂件⼤⼩限制。
单个⽂件最⼤限制:spring.servlet.multipart.max-file-size=50Mb
单次请求最⼤限制:spring.servlet.multipart.max-request-size=70Mb
总结:本⽂功能较为简单,所以有些过程并没有更细致过程以及规范代码,⽐如存放路径采⽤项⽬路径,新⽂件名保持和原⽂件后缀⼀致等,需要的⼩伙伴可以根据⾃⼰业务进⾏修改。
续更,总觉得代码过于随意了,补充⽂件上传获得⽂件后缀相关函数
private String getFileSuffix(MultipartFile file) {
if (file == null) {
return null;
}
String fileName = OriginalFilename();
int suffixIndex = fileName.lastIndexOf(".");
if (suffixIndex == -1) { // ⽆后缀
return null;
} else {  // 存在后缀
return fileName.substring(suffixIndex, fileName.length());
}
}
在随机⽣成⽂件名后补充如下代码即可,如果返回⽂件后缀不为空则将其加⼊新产⽣的⽂件名中即可:
String fileSuffix = getFileSuffix(zipFile);
if (fileSuffix != null) { // 拼接后缀
fileName += fileSuffix;
}
File targetFile = new File(targetFilePath + File.separator + fileName);
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。