package com.common.util; import org.apache.commons.lang3.StringUtils; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.File; /** * @Description 本地文件上传 * @Author FangYN * @Date 2019/12/25 **/ public class FileUtils { public static final String imagePath = "/upload/image/"; /** * 上传文件至本地 * 存储原图地址如:http://127.0.0.1:8080:/upload/image/20170804/1501811601145.jpg * @param muFile * @param pathFileName 文件夹名 * @param fileName 文件名 * @return 返回 图片保存路径 具体保存地址为 C:/upload/image/ 下 */ public static String fileUpload(HttpServletRequest request, MultipartFile muFile, String pathFileName, String fileName) { //获取上传文件的保存路径:文件拼接地址:服务器本地保存路径+文件名+后缀 String fileNamess = null; String getFileName = null; try { //文件路径如:C:/upload/image/文件名/ String picPath = getUploadFilePath(request) + imagePath + pathFileName + "/"; //根据真实路径创建目录文件 File picSaveFile = new File(picPath); if(!picSaveFile.exists()) picSaveFile.mkdirs(); fileNamess = picPath + fileName ; File file = new File(fileNamess); muFile.transferTo(file);// 转存文件到指定的路径 getFileName = imagePath + pathFileName + "/" + fileName; System.out.println("图片本地保存地址:" + (fileNamess)); System.out.println("图片地址:" + (getFileName)); } catch (Exception e) { e.printStackTrace(); System.out.println("创建本地文件失败"+e.getMessage()); } return getFileName; } /** * 文件删除 * @param fileNameUrl 需要删除的文件名称(完整的访问路径) * @return 是否删除成功 */ public static void fileDelete(HttpServletRequest request, String fileNameUrl){ if(fileNameUrl == null || fileNameUrl.equals("")) return; String url = fileNameUrl.substring(0, fileNameUrl.lastIndexOf(imagePath)); fileNameUrl = fileNameUrl.replace(url, getUploadFilePath(request)); try{ File f = new File(fileNameUrl); f.delete(); }catch(Exception ex){ ex.printStackTrace(); System.out.println("文件删除出现错误!"); } } /** * 文件重命名/移动文件 * @param oldFileNameUrl * @param newFileName * @param filePath 文件夹 * @return 图片保存路径(相对路径) */ public static String fileRename(HttpServletRequest request, String oldFileNameUrl, String newFileName, String filePath){ if(StringUtils.isBlank(newFileName) || StringUtils.isBlank(oldFileNameUrl)) return ""; String diskPath = getUploadFilePath(request); String path = diskPath + imagePath + filePath + "/"; //根据真实路径创建目录文件 File picSaveFile = new File(path); if(!picSaveFile.exists()) picSaveFile.mkdirs(); String url = oldFileNameUrl.substring(0, oldFileNameUrl.lastIndexOf(imagePath)); oldFileNameUrl = StringUtils.isBlank(url) ? diskPath+oldFileNameUrl : oldFileNameUrl.replace(url, diskPath); try{ File file=new File(oldFileNameUrl); if(file.exists()) { Integer suffixLength = oldFileNameUrl.lastIndexOf("."); String suffix = ""; if (suffixLength != -1) { suffix = oldFileNameUrl.substring(oldFileNameUrl.lastIndexOf(".")); } String newFileNameUrl = path + newFileName + suffix; file.renameTo(new File(newFileNameUrl)); return imagePath + filePath + "/" + newFileName + suffix; } }catch(Exception ex){ ex.printStackTrace(); System.out.println("文件重命名出现错误!"); } return ""; } public static Boolean isFileExist(HttpServletRequest request, String fileName, String filePath) { if (StringUtils.isBlank(fileName)) return false; String diskPath = getUploadFilePath(request); String path = diskPath + imagePath + filePath + "/"; //根据真实路径创建目录文件 File picSaveFile = new File(path); if (!picSaveFile.exists()) picSaveFile.mkdirs(); try { String fileNameUrl = path + fileName; File file = new File(fileNameUrl); if (file.exists()) { return true; } } catch (Exception ex) { ex.printStackTrace(); } return false; } /** * 获取当前项目所在根目录地址 * @param request * @return */ private static String getUploadFilePath(HttpServletRequest request){ String savePath = request.getSession().getServletContext().getRealPath(""); savePath = savePath.substring(0,savePath.lastIndexOf("\\")); return savePath+"/"; } }
Java-本地文件上传、重命名、移动、删除
最新推荐文章于 2024-07-16 05:01:25 发布