java本地文件复制
String strFilePath="本地路径"
1.获取文件名
File tempFile =new File( strFilePath.trim());
String fileName = tempFile.getName();
2.复制本地文件
方法一:
String mburl="docmb\\temp.doc";
String dest ="目标路径" //精确到文件名
File source = new File(mburl);
File dest = new File(zwurl);
try {
FileUtils.copyFile(source, dest); //路径中无文件夹 自动创建
} catch (IOException e) {
e.printStackTrace();
}
方法二:
3.检测路径是否合法
File file = new File(path);
if (!file.exists())
{}
4.网页下载服务器文件方法
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");
String fileName = tempFile.getName();
response.addHeader("Content-Disposition", "attachment; filename*=utf-8'zh_cn'"+fileName);
InputStream inStream=new FileInputStream(strFilePath);// 文件的存放路径
byte[] b = new byte[1024];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
【推荐】AI 的力量,开发者的翅膀:欢迎使用 AI 原生开发工具 TRAE
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
· 博客园出海记-开篇:扬帆启航
· 微软开源的 MCP 教程「GitHub 热点速览」
· 记一次 .NET 某汽车控制焊接软件 卡死分析
· 关于布尔类型的变量不要加 is 前缀,被网友们吐槽了,特来完善下
· C#中的多级缓存架构设计与实现深度解析