package com.yanning.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class Test {
public static void main(String[] args) {
Test.uploadFile(new File("d://11.sql"), "d://", "12.sql");
}
/**
* 文件上传
*
* @param file
* 要上传的文件
* @param path
* 保存的文件路径
* @param fileName
* 保存的文件名称
* @return boolean
*/
public static boolean uploadFile(File file, String path, String fileName) {
boolean b = true;
try {
String root = path;
File f = new File(root);
if (!f.isDirectory()) {
f.mkdirs();
}
InputStream is = new FileInputStream(file);
File destFile = new File(root, fileName);
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[1025];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
is.close();
os.close();
} catch (Exception e) {
b = false;
e.printStackTrace();
}
return b;
}
}
java上传文件
最新推荐文章于 2025-10-17 07:09:01 发布
本文提供了一个使用Java实现文件上传的简单示例。通过该示例,你可以了解如何读取本地文件并将其复制到指定目录的过程。代码展示了如何处理文件输入输出流以及异常情况。

5808

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



