发布于2020-11-19 20:54 阅读(1001) 评论(0) 点赞(24) 收藏(4)
package com.huilan.controller;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.huilan.utils.UploadFileUtils;
@RestController
public class UploadTest {
private static final Logger log = LoggerFactory.getLogger(UploadTest.class);
//@ResponseBody
// @RequestMapping("/fileupload")
public Map upload(@RequestParam("file")MultipartFile imgFile,HttpServletRequest request,HttpSession session) throws Exception{
Map msg = new HashMap<>();
if (imgFile.isEmpty()) {
// 设置错误状态码
// msg.setCode(500);
// msg.setMsg("上传失败,请选择文件");
return msg;
}
// 拿到文件名
String filename = imgFile.getOriginalFilename();
String newFileName = UploadFileUtils.getTime(UploadFileUtils.YYYYMMDDHHMMSSSSS)+System.currentTimeMillis()+filename.substring(filename.indexOf("."));
// 存放上传图片的文件夹
File fileDir = getImgDirFile();
// 输出文件夹绝对路径 -- 这里的绝对路径是相当于当前项目的路径而不是“容器”路径
System.out.println(fileDir.getAbsolutePath());
try {
// 构建真实的文件路径
File newFile = new File(fileDir.getAbsolutePath() + File.separator + newFileName);
System.out.println(newFile.getAbsolutePath());
// 上传图片到 -》 “绝对路径”
imgFile.transferTo(newFile);
// msg.setMsg("上传成功!");
} catch (IOException e) {
e.printStackTrace();
}
return msg;
}
// 项目根路径下的目录 -- SpringBoot static 目录相当于是根路径下(SpringBoot 默认)
public final static String IMG_PATH_PREFIX = "templates/static/upload/imgs/";
public static File getImgDirFile() throws Exception{
// 构建上传文件的存放 "文件夹" 路径
String fileDirPath = new String("src/main/resources/" + IMG_PATH_PREFIX);
String filePath =UploadFileUtils.getTime("yyyy-MM-dd");
String path = fileDirPath+filePath;
System.out.println(path);
File fileDir = new File(path);
if(!fileDir.exists()){
// 递归生成文件夹
fileDir.mkdirs();
}
return fileDir;
}
///static/upload/imgs/2020-11-17/202011171802400901605607360090.jpg
public final static String IMG_PATH = "static/upload/imgs/";
@ResponseBody
@RequestMapping("/fileupload")
public String fileUpload(@RequestParam("file")MultipartFile imgFile,HttpServletRequest request) throws Exception{
// Map msg = new HashMap<>();
if (imgFile.isEmpty()) {
log.info("上传成功!");
return null;
}
// 获取文件名
String filename = imgFile.getOriginalFilename();
String newFileName = UploadFileUtils.getTime(UploadFileUtils.YYYYMMDDHHMMSSSSS)+System.currentTimeMillis()+filename.substring(filename.indexOf("."));
// 存放上传图片的文件夹
String fileDirPath = new String("src/main/resources/templates/" + IMG_PATH);
//文件日期
String filePath =UploadFileUtils.getTime("yyyy-MM-dd");
String path = fileDirPath+filePath;
String resultUrl = IMG_PATH+filePath+"/"+newFileName;
System.out.println(path);
System.out.println("返回:"+resultUrl);
File fileDir = new File(path);
//创建文件
if(!fileDir.exists()){
fileDir.mkdirs();
}
System.out.println(fileDir.getAbsolutePath());
try {
File newFile = new File(fileDir.getAbsolutePath() + File.separator + newFileName);
System.out.println(newFile.getAbsolutePath());
imgFile.transferTo(newFile);
log.info("上传成功!");
} catch (IOException e) {
e.printStackTrace();
}
return resultUrl;
}
}
原文链接:https://blog.csdn.net/shijinjins/article/details/109751731
作者:java战神
链接:http://www.javaheidong.com/blog/article/962/fecbc044c4c5b3175330/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!