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();
            }

方法二:

// 创建字节输入流对象并关联文件路径
FileInputStream fis = new FileInputStream("c:\\c.png");
// 利用字节输出流对象创建高效字节输出流对象
BufferedInputStream bis = new BufferedInputStream(fis);
// 创建字节输出流对象并指定文件路径。
FileOutputStream fos = new FileOutputStream("d:\\c.png");
// 利用字节输出流创建高效字节输出流对象
BufferedOutputStream bos = new BufferedOutputStream(fos);
// 定义字节数组接收读取的字节
byte[] buffer = new byte[1024];
// 定义变量接收读取的字节数
int len = -1;
// 循环读取图片数据
while((len = bis.read(buffer)) != -1) {
// 每读取一个字节的数据就写出到目标文件中
bos.write(buffer,0,len);
}
// 关闭流
bis.close();
bos.close();

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();
        }