JAVA从局域网共享文件夹中下载上传文件
先下载jar包,
http://jcifs.samba.org/
- 从共享目录下载文件
- public static void smbGet(String remoteUrl,String localDir) {
- InputStream in = null;
- OutputStream out = null;
- try {
- SmbFile remoteFile = new SmbFile(remoteUrl);
- if(remoteFile==null){
- System.out.println("共享文件不存在");
- return;
- }
- String fileName = remoteFile.getName();
- 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();
- }
- }
- }
- public static void smbGet(String remoteUrl,String localDir) {
- InputStream in = null;
- OutputStream out = null;
- try {
- SmbFile remoteFile = new SmbFile(remoteUrl);
- if(remoteFile==null){
- System.out.println("共享文件不存在");
- return;
- }
- String fileName = remoteFile.getName();
- 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();
- }
- }
- }
-
- 向共享目录上传文件
- public static void smbPut(String remoteUrl,String localFilePath) {
- InputStream in = null;
- OutputStream out = null;
- try {
- File localFile = new File(localFilePath);
-
- String fileName = localFile.getName();
- SmbFile remoteFile = new SmbFile(remoteUrl+"/"+fileName);
- in = new BufferedInputStream(new FileInputStream(localFile));
- out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
- 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();
- }
- }
- }
远程url smb://192.168.0.77/test若是须要用户名密码就这样:
smb://username:password@192.168.0.77/test
欢迎关注本站公众号,获取更多信息