UploadVideoController:
package com.lianshang.platform.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
@Controller
public class UploadVideoController {
//完整访问路径
private String url;
//访问路径前缀
private String ip = "http://";
//上传
@RequestMapping(value = "/uploadFile", produces = "application/json;charset=UTF-8")
@ResponseBody
public String uploadFile(@RequestParam("fileName") MultipartFile file, HttpServletRequest request) {
String host = request.getLocalAddr();
Integer port =request.getLocalPort();
System.out.print("服务器id"+host+"\n"+"端口号:"+port);
System.out.print("上传文件===" + "\n");
//判断文件是否为空
if (file.isEmpty()) {
return "上传文件不可为空";
}
// 获取文件名
String fileName = file.getOriginalFilename();
// System.out.print("上传的文件名为: "+fileName+"\n");
fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "_" + fileName;
System.out.print("(加时间戳,尽量避免文件名称重复)保存的文件名为: " + fileName + "\n");
//加个时间戳,尽量避免文件名称重复
// System.getProperty("user.dir") 获取项目根路径
String path =System.getProperty("user.dir")+"\\fileUpload\\" + fileName;
//文件绝对路径
System.out.print("保存文件绝对路径" + path + "\n");
//创建文件路径
File dest = new File(path);
//判断文件是否已经存在
if (dest.exists()) {
return "文件已经存在";
}
//判断文件父目录是否存在
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdir();
}
InputStream inputStream=null;
OutputStream outputStream=null;
try {
//上传文件
inputStream = file.getInputStream();
byte[] bytes = new byte[1024 * 5];
outputStream = new FileOutputStream(dest);
while (inputStream.read(bytes) != -1) {
outputStream.write(bytes);
outputStream.flush();
}
//拼接服务器地址
url = ip+host+":"+port+"/"+path;
} catch (IOException e) {
e.printStackTrace();
return "上传失败";
}
return "上传成功,文件url==" + url;
}
}
WebConfig:
package com.lianshang.platform.config;
import com.lianshang.platform.annotation.AuthMethodArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.*;
import javax.servlet.MultipartConfigElement;
import java.util.List;
@Configuration
public class WebConfig implements WebMvcConfigurer
{
/**
* 在配置文件中配置的文件保存路径
*/
@Value("file:/fileUpload/")
private String mImagesPath;
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//文件最大KB,MB
factory.setMaxFileSize("1024MB");
//设置总上传数据总大小
factory.setMaxRequestSize("1024MB");
return factory.createMultipartConfig();
}
}