자바 SmbFile 클래스 를 통 해 공유 폴 더 의 예제
pom.xml 폴 더 에 다음 과 같은 의존 도 를 추가 하면 SmbFile 류 의 jar 패 키 지 를 참조 할 수 있 습 니 다.
<dependency>
  <groupId>jcifs</groupId>
  <artifactId>jcifs</artifactId>
  <version>1.3.17</version>
</dependency>파일 읽 기
/**
 *              (   )   
 * @param remoteUrl
 */
public static void getSharedFileList(String remoteUrl) {
 SmbFile smbFile;
 try {
  // smb://userName:passWord@host/path/
  smbFile = new SmbFile(remoteUrl);
  if (!smbFile.exists()) {
   System.out.println("no such folder");
  } else {
   SmbFile[] files = smbFile.listFiles();
   for (SmbFile f : files) {
    System.out.println(f.getName());
   }
  }
 } catch (MalformedURLException e) {
  e.printStackTrace();
 } catch (SmbException e) {
  e.printStackTrace();
 }
}3.폴 더 만 들 기
/**
 *      
 * @param remoteUrl 
 * @param folderName
 * @return
 */
public static void smbMkDir(String remoteUrl, String folderName) {
 SmbFile smbFile;
 try {
  // smb://userName:passWord@host/path/folderName
  smbFile = new SmbFile(remoteUrl + folderName);
  if (!smbFile.exists()) {
   smbFile.mkdir();
  }
 } catch (MalformedURLException e) {
  e.printStackTrace();
 } catch (SmbException e) {
  e.printStackTrace();
 }
}파일 업로드
/**
 *     
 * @param remoteUrl
 * @param shareFolderPath
 * @param localFilePath
 * @param fileName
 */
public static void uploadFileToSharedFolder(String remoteUrl, String shareFolderPath, String localFilePath, String fileName) {
 InputStream inputStream = null;
 OutputStream outputStream = null;
 try {
  File localFile = new File(localFilePath);
  inputStream = new FileInputStream(localFile);
  // smb://userName:passWord@host/path/shareFolderPath/fileName
  SmbFile smbFile = new SmbFile(remoteUrl + shareFolderPath + "/" + fileName);
  smbFile.connect();
  outputStream = new SmbFileOutputStream(smbFile);
  byte[] buffer = new byte[4096];
  int len = 0; //     
  while ((len = inputStream.read(buffer, 0, buffer.length)) != -1) {
   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();
 }
}폴 더 를 삭제 하면 삭제 할 폴 더 에 경 로 를 가리 키 면 됩 니 다.자바 가 SmbFile 류 를 통 해 공유 폴 더 를 조작 하 는 것 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 자바 작업 공유 폴 더 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 바 랍 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.