• 欢迎光临~

java本地文件读写

开发技术 开发技术 2022-01-23 89次浏览

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[100];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}

程序员灯塔
转载请注明原文链接:java本地文件读写
喜欢 (0)
违法和不良信息举报电话:022-22558618 举报邮箱:dljd@tidljd.com