写点什么

java 通过 SmbFile 类操作共享文件夹,膜拜大牛

发布于: 刚刚
        outputStream.write(buffer, 0, len);
}
// 刷新缓冲的输出流
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
复制代码


}





五、下载文件
------
复制代码


/**


  • 下载文件到浏览器

  • @param httpServletResponse

  • @param remoteUrl

  • @param shareFolderPath

  • @param fileName


*/


public static void downloadFileToBrowser(HttpServletResponse httpServletResponse, String remoteUrl, String shareFolderPath, String fileName) {


SmbFile smbFile;
SmbFileInputStream smbFileInputStream = null;
OutputStream outputStream = null;
try {
// smb://userName:passWord@host/path/shareFolderPath/fileName
smbFile = new SmbFile(remoteUrl + shareFolderPath + "/" + fileName);
smbFileInputStream = new SmbFileInputStream(smbFile);
httpServletResponse.setHeader("content-type", "application/octet-stream");
httpServletResponse.setContentType("application/vnd.ms-excel;charset=UTF-8");
httpServletResponse.setHeader("Content-disposition", "attachment; filename=" + fileName);
// 处理空格转为加号的问题
httpServletResponse.setHeader("Content-Disposition", "attachment; fileName=" + fileName + ";filename*=utf-8''" + URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20"));
outputStream = httpServletResponse.getOutputStream();
byte[] buff = new byte[2048];
int len;
while ((len = smbFileInputStream.read(buff)) != -1) {
outputStream.write(buff, 0, len);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (SmbException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
outputStream.close();
smbFileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
复制代码


}



/**
* 下载文件到指定文件夹
* @param remoteUrl
* @param shareFolderPath
* @param fileName
* @param localDir
*/
public static void downloadFileToFolder(String remoteUrl, String shareFolderPath, String fileName, String localDir) {
InputStream in = null;
OutputStream out = null;
try {
SmbFile remoteFile = new SmbFile(remoteUrl + shareFolderPath + File.separator + fileName);
File localFile = new File(localDir + File.separator + fileName);
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
out = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}```


六、删除文件
------```
/**
* 删除文件
* @param remoteUrl
* @param shareFolderPath
* @param fileName
*/
public static void deleteFile(String remoteUrl, String shareFolderPath, String fileName) {
SmbFile SmbFile;
try {
// smb://userName:passWord@host/path/shareFolderPath/fileName
SmbFile = new SmbFile(remoteUrl + shareFolderPath + "/" + fileName);
if (SmbFile.exists()) {
SmbFile.delete();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (SmbException e) {
e.printStackTrace();
}
}```


删除文件夹将路径指向要删除的文件夹即可。


往期精彩内容:


[Java知识体系总结(2021版)](https://gitee.com/vip204888/java-p7)


[超详细的springBoot学习笔记](https://gitee.com/vip204888/java-p7)


[Java多线程基础知识总结(绝对经典)](https://gitee.com/vip204888/java-p7)


[Java面试题总结(附答案)](https://gitee.com/vip204888/java-p7)



# 总结
在这里,由于面试中MySQL问的比较多,因此也就在此以MySQL为例为大家总结分享。但是你要学习的往往不止这一点,还有一些主流框架的使用,Spring源码的学习,Mybatis源码的学习等等都是需要掌握的,我也把这些知识点都整理起来了,有需要的朋友可以**[【转发+关注】后点击这里免费领取!](https://gitee.com/vip204888/java-p7)**
![面试真题](https://static001.geekbang.org/infoq/6a/6a529e45abc6ea5ff77d0877a6ffc2a3.jpeg)

![Spring源码笔记](https://static001.geekbang.org/infoq/78/782d2884e8ed2d1bded8dcacfcc7c6ae.jpeg)
复制代码


用户头像

VX:Lzzzzzz63 领取资料 2021.07.07 加入

还未添加个人简介

评论

发布
暂无评论
java 通过 SmbFile 类操作共享文件夹,膜拜大牛