String fname = item.getName();
//根据文件原始名称,创建新文件名
String newFileName = UploadUtils.getUUIDName(fname);
//获取到存放图片根目录真实路径
String rootPath = getServletContext().getRealPath("/products/3");
//#_创建随机8层目录
String path01 = UploadUtils.getDir(newFileName);
File ranDir = new File(rootPath + path01);
if (!ranDir.exists()) {
ranDir.mkdirs();
}
//#_创建文件
File file = new File(ranDir, newFileName);
if (!file.exists()) {
file.createNewFile();
}
//#_流对接,通过item.getInputStream();将图片数据输出到文件中
OutputStream os = new FileOutputStream(file);
InputStream is = item.getInputStream();
IOUtils.copy(is, os);
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
转载于:https://www.cnblogs.com/lq123/p/10309939.html
本文详细介绍了一种文件上传的处理方法,包括如何生成新的文件名、创建随机目录、处理文件流等关键步骤,确保文件安全有效地上传到服务器。

150

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



