/**
* 读取字符流
*
* @param request
* @return
*/
public static String readBuffer(HttpServletRequest request) {
try (BufferedReader reader = request.getReader()) {
return reader.lines().collect(Collectors.joining("\n"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 文件重命名并移动到指定文件夹
*
* @param file
* @param saveDire
* @param fileName
* @return
*/
public String upload(File file, String saveDire, String fileName) {
try {
// 获取要存放的文件夹对象
Path targetDirectory = Paths.get(Constant.UPLOAD_BASE_URL + saveDire);
// 如果该文件夹不存在则创建
if (!Files.isDirectory(targetDirectory)) {
Files.createDirectories(targetDirectory);
}
// 重命名并移动文件到指定文件夹
Files.move(file.toPath(), targetDirectory.resolve(fileName), StandardCopyOption.REPLACE_EXISTING);
// 拼接文件访问路径
return Constant.FILE_ACCESS_URL + "/" + saveDire + "/" + fileName;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
感谢分享