import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.nio.file.Files;
import java.util.*;
@RestController
@RequestMapping("/file")
public class UploadAction {
/**
* 单视频文件上传
*
* @param file
* @param request
* @return
*/
@PostMapping("/upload_video")
@ResponseBody
public Map upload_video(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
FileInputStream in = null;
//获取跟目录
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if(!path.exists()) path = new File("");
System.out.println("path:"+path.getAbsolutePath());
if (!file.isEmpty()) {
String saveFileName = file.getOriginalFilename();
System.out.println("文件名:"+saveFileName);
File file1 = new File(path.getAbsolutePath(),"static/video/");
File saveFile = new File(path.getAbsolutePath(),"static/video/"+saveFileName);
if (!file1.exists()) {
file1.mkdirs();
}
if (saveFile.exists()){
Files.delete(saveFile.toPath());
}
System.out.println("upload url:"+saveFile.getAbsolutePath());
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(saveFile));
out.write(file.getBytes());
out.flush();
out.close();
Map map= new HashMap();
map.put("code",0);
map.put("msg","上传成功!");
map.put("src","/video/"+saveFileName);
return map ;
} catch (FileNotFoundException e) {
e.printStackTrace();
return (Map) Collections.singletonList(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
return (Map) Collections.singletonList(e.getMessage());
}
} else {
return (Map) Collections.singletonList("上传失败,因为文件为空.");
}
}
/**
* 单图片文件上传
*
* @param file
* @param request
* @return
*/
@RequestMapping(value = "/upload_img")
@ResponseBody
public Map upload_img(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
//获取跟目录
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if(!path.exists()) path = new File("");
System.out.println("path:"+path.getAbsolutePath());
if (!file.isEmpty()) {
String saveFileName = file.getOriginalFilename();
System.out.println("文件名:"+saveFileName);
File file1 = new File(path.getAbsolutePath(),"static/images/");
File saveFile = new File(path.getAbsolutePath(),"static/images/"+saveFileName);
if (!file1.exists()) {
file1.mkdirs();
}
if (saveFile.exists()){
Files.delete(saveFile.toPath());
}
System.out.println("upload url:"+saveFile.getAbsolutePath());
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(saveFile));
out.write(file.getBytes());
out.flush();
out.close();
Map map= new LinkedHashMap();
Map map2 = new HashMap();
map.put("code",0);
map.put("msg","图片上传成功!");
map2.put("src","/images/"+saveFileName);
map2.put("title",saveFileName);
map.put("data",map2);
return map ;
} catch (FileNotFoundException e) {
e.printStackTrace();
Map map= new HashMap();
map.put("e",e.getMessage());
return map;
} catch (IOException e) {
e.printStackTrace();
Map map= new HashMap();
map.put("e",e.getMessage());
return map;
}
} else {
Map map= new HashMap();
map.put("e","上传失败,因为文件为空.");
return map;
}
}
/**
* 多文件上传
*
* @param request
* @return
*/
@PostMapping("/uploadFiles")
@ResponseBody
public String uploadFiles(HttpServletRequest request) throws IOException {
File savePath = new File(request.getSession().getServletContext().getRealPath("/upload/"));
if (!savePath.exists()) {
savePath.mkdirs();
}
List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
MultipartFile file = null;
BufferedOutputStream stream = null;
for (int i = 0; i < files.size(); ++i) {
file = files.get(i);
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
File saveFile = new File(savePath, file.getOriginalFilename());
stream = new BufferedOutputStream(new FileOutputStream(saveFile));
stream.write(bytes);
stream.close();
} catch (Exception e) {
if (stream != null) {
stream.close();
stream = null;
}
return "第 " + i + " 个文件上传有错误" + e.getMessage();
}
} else {
return "第 " + i + " 个文件为空";
}
}
return "所有文件上传成功";
}
}
spring bootweb视频图片上传
最新推荐文章于 2024-05-14 16:41:22 发布