smb上传图片工具类

package com.zfq.springcloud.controller;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* @descriptions: 上传共享文件夹
* @author: FUQIANG.ZHOU
* @date: 2021/2/3 23:09
* @version: 1.0
*/
public class SmbUtil {


/** 文件上传
* String localFilePath:要上传的本地文件路径
* String path:远程服务器共享文件夹名称
* String username:远程服务器用户名
* String password:远程服务器密码
* */
public static void upload(MultipartFile localFile, String path, String username, String password) {
OutputStream out = null;
try (InputStream in = localFile.getInputStream()) {

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, username, password);
String remoteUrl = "smb:" + path + (path.endsWith(File.separator) ? "" : File.separator);
SmbFile remoteFile = new SmbFile(remoteUrl + File.separator + localFile.getName(), auth);
remoteFile.connect();
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
byte[] buffer = new byte[4096];
int len = 0;
while ((len = in.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
}
}
}

private void responseFileStream(HttpServletResponse response, String filePath){
ServletOutputStream out = null;
FileInputStream in = null;
try {

in = new FileInputStream(new File(filePath));
String[] dir = filePath.split("/");
String fileName = dir[dir.length-1];
String[] array = fileName.split("[.]");
String fileType = array[array.length-1].toLowerCase();
//设置文件ContentType类型//图片类型
if("jpg,jepg,gif,png".contains(fileType)){
response.setContentType("image/"+fileType);
//pdf类型
}else if("pdf".contains(fileType)){
response.setContentType("application/pdf");
}else{//自动判断下载文件类型
response.setContentType("multipart/form-data");
}
//设置文件头:最后一个参数是设置下载文件名
response.setHeader("Content-Disposition", "attachment;fileName="+fileName);
out = response.getOutputStream();
// 读取文件流
int len = 0;
byte[] buffer = new byte[1024 * 10];
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
} catch (FileNotFoundException e) {
} catch (Exception e) {
} finally {
try {
out.close();
in.close();
} catch (NullPointerException e) {
} catch (Exception e) {
}
}
}

/**
* 文件下载
* String localFilePath:本地文件夹
* String path:远程服务器共享文件名
* String username:远程服务器用户名
* String password:远程服务器密码
*/
public static void download2(HttpServletResponse response, String path, String username, String password) {
InputStream in = null;
try (OutputStream out = response.getOutputStream()){
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, username, password);
String remoteUrl = "smb:" + path + (path.endsWith("/") ? "" : "/");
SmbFile remoteFile = new SmbFile(remoteUrl, auth);
remoteFile.connect();
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
byte[] buffer = new byte[4096];
int len = 0;
while ((len = in.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
}

public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
System.out.println(now.format(dtf));
//LocalDateTime localDateTime = new LocalDateTime;
// localDateTime.format("yyyyMMddHHmmss");
}

}

posted @ 2021-02-04 01:00  奥里给  阅读(188)  评论(0编辑  收藏  举报