java实现上传文件到本地

private static final String IMG_PATH = "D:\\upload\\resources\\images\\";
	private static final String DOC_PATH = "D:\\upload\\resources\\doc\\";

	public static String upload(MultipartFile file) throws Exception {
		String filename = file.getOriginalFilename();
		String suffix = filename.substring(filename.lastIndexOf(".")).toLowerCase();
		String partPath = "";
		String pathName;
		if (img_check(suffix)) {
			if (file.getSize() > 2 * 1024 * 1024) {
				throw new IllegalArgumentException("上传文件大小不能超过2M");
			}
			partPath = IMG_PATH;
			pathName = "images";
		} else if (doc_check(suffix)) {
			if (file.getSize() > 10 * 1024 * 1024) {
				throw new IllegalArgumentException("上传文件大小不能超过10M");
			}
			partPath = DOC_PATH;
			pathName = "doc";
		} else {
			throw new IllegalArgumentException("不支持的文件格式");
		}
		return uploadResources(file, partPath, pathName);
	}


	public static String uploadResources(MultipartFile file, String path, String pathName) {
		// 指定本地磁盘的盘符和路径
		File folder = new File(path);
		// 检查文件夹是否存在
		if (!folder.exists()) {
			folder.mkdirs();
		}
		String fileName = file.getOriginalFilename();
		String suffix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
		img_check(suffix);
		// 构建保存文件的路径
		String uuidName = uuidName(fileName);
		try {
			file.transferTo(new File(path + uuidName));
			return "http://" + InetAddress.getLocalHost().getHostAddress() + "/resources/" + pathName + "/" + uuidName;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}


	public static String uploadLocalResources(String filePathName) {
		try {
			String suffix = filePathName.substring(filePathName.lastIndexOf(".")).toLowerCase();
			String filePath = "";
			String fileType;
			if (img_check(suffix)) {
				filePath = IMG_PATH;
				fileType = "images";
			} else if (doc_check(suffix)) {
				filePath = DOC_PATH;
				fileType = "doc";
			} else {
				return null;
			}
			File file = new File(filePath);
			//如果文件目录不存在,就执行创建
			if (!file.isDirectory()) {
				file.mkdirs();
			}
			//创建目标文件
			String uuidFileName = uuidName(FilenameUtils.getName(filePathName));
			String makeFilePath = filePath + uuidFileName;
			try {
				File targetFile = new File(makeFilePath);
				//读取本地文件
				File localFile = new File(filePathName);
				//获取本地文件输入流
				InputStream stream = new FileInputStream(localFile);
				//写入目标文件
				byte[] buffer = new byte[1024 * 1024];
				FileOutputStream fos = new FileOutputStream(targetFile);
				int byteRead = 0;
				while ((byteRead = stream.read(buffer)) != -1) {
					fos.write(buffer, 0, byteRead);
					fos.flush();
				}
				fos.close();
				stream.close();
				return "http://" + InetAddress.getLocalHost().getHostAddress() + "/resources/" + fileType + "/" + uuidFileName;
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @Description: uuid命名
	 */
	public static String uuidName(String filename) {
		filename = UUID.randomUUID().toString().replace("-", "") + filename.substring(filename.lastIndexOf("."));
		return filename;
	}

	private static boolean img_check(String targetValue) {
		String[] arr = new String[]{".jpg", ".jpeg", ".png", ".gif"};
		return Arrays.asList(arr).contains(targetValue);
	}

	public static boolean doc_check(String targetValue) {
		String[] arr = new String[]{".doc", ".pdf", ".docx"};
		return Arrays.asList(arr).contains(targetValue);
	}

你可能感兴趣的:(java,开发语言)