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

上一篇:Android 设置默认Launcher,银行软件开发面试题


下一篇:第七章 监控系统-zabbix自定义监控项入门