注意路径:可以从磁盘中找出文件夹具体路径拿出来就可以使用了‘’
代码:
package gaea.ImportService.utils;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Properties;
public class uploads {
private static final Logger logger = LoggerFactory.getLogger(uploads.class);
public static String logUpload(MultipartFile file) throws Exception {
if (file.isEmpty()) {
return "上传失败,请选择文件";
}
String rootPath = System.getProperty("user.dir");
String fileName = file.getOriginalFilename();
String filePath = rootPath + "\\src\\4.Service\\Gaea.PreNucleusGt.Boot\\src\\main\\resources\\UploadsFile\\";
File dest = new File(filePath+fileName);
try {
file.transferTo(dest);
logger.info("上传成功");
return "上传成功";
} catch (IOException e) {
logger.error(e.toString(), e);
}
return "上传失败!";
}
}
本文介绍如何在Java中将本地文件上传到项目文件夹。通过使用Spring的相关工具类,如ClassPathResource,ResourceUtils等,可以方便地获取文件路径并进行上传操作。
386

被折叠的 条评论
为什么被折叠?



