修改文件上传功能

This commit is contained in:
XaoLi717 2024-10-18 14:47:05 +08:00
parent b6a15cbc56
commit 7265a29a6c

View File

@ -9,6 +9,9 @@ import java.io.*;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileUtil { public class FileUtil {
/** /**
@ -17,14 +20,14 @@ public class FileUtil {
* @param outPath * @param outPath
* @throws Exception * @throws Exception
*/ */
public static void saveFile(InputStream in, String outPath) throws Exception { public static void saveFile(InputStream in, String outPath,String filePath) throws Exception {
// FileChannel in = new FileInputStream("src/demo20/data.txt").getChannel(), // System.out.println("saveFile"+in+" "+outPath+" "+filePath);
// out = new FileOutputStream("src/demo20/data2.txt").getChannel(); Path dcPath = Paths.get(filePath); // 创建 Path 对象以表示要创建的文件路径
// in.transferTo(0, in.size(), out); //路径不存在的话创建路径
// OutputStream osm = new FileOutputStream(outPath); if (!Files.exists(dcPath)){
// IOUtils.copy(in, osm); Files.createDirectories(dcPath);
System.out.println("saveFile"+in+" "+outPath); }
try (OutputStream osm = new FileOutputStream(outPath)) { try (OutputStream osm = Files.newOutputStream(Paths.get(outPath))) {
IOUtils.copy(in, osm); IOUtils.copy(in, osm);
} }