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上传文件
最新推荐文章于 2023-09-15 16:24:43 发布