package com.fairyland.jdp.exam.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
public class SmbUtil {
// 1. 声明属性
private String url = “smb://HRE”;
private SmbFile smbFile = null;
private SmbFileOutputStream smbOut = null;
private static SmbUtil smbUtil = null; // 共享文件协议
private SmbUtil() {
this.init();
}
// 2. 得到SmbUtil和连接的方法
public static synchronized SmbUtil getInstance() {
if (smbUtil == null)
return new SmbUtil();
return smbUtil;
}
// 3.smbFile连接
public void init() {
try {
smbFile = new SmbFile(this.url);
smbFile.connect();
} catch (MalformedURLException e) {
e.printStackTrace();
System.out.print(e);
} catch (IOException e) {
e.printStackTrace();
System.out.print(e);
}
}
// 4.上传文件到服务器
public int uploadFile(File file) throws ExamException {
int flag = -1;
BufferedInputStream bf = null;
try {
this.smbOut = new SmbFileOutputStream(this.url + "/" + file.getName(), false);
bf = new BufferedInputStream(new FileInputStream(file));
byte[] bt = new byte[8192];
int n = bf.read(bt);
while (n != -1) {
this.smbOut.write(bt, 0, n);
this.smbOut.flush();
n = bf.read(bt);
}
flag = 0;
} catch (SmbException e) {
throw new ExamException(e.getMessage());
} catch (MalformedURLException e) {
throw new ExamException(e.getMessage());
} catch (UnknownHostException e) {
throw new ExamException(e.getMessage());
} catch (IOException e) {
throw new ExamException(e.getMessage());
} finally {
try {
if (null != this.smbOut)
this.smbOut.close();
if (null != bf)
bf.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return flag;
}
// 4.上传文件到服务器
public Map<String,Object> uploadFile(MultipartFile file) throws ExamException {
Map<String,Object> resultMap=new HashMap<>();
if (file.isEmpty()) {
throw new ExamException("上传文件为空!");
}
String fileName = file.getOriginalFilename();
fileName = new SimpleDateFormat("YYYYMMddHHmmss").format(new Date()) + "_" + fileName;
BufferedInputStream bf = null;
String filePath=this.url + File.separator + fileName;
try {
this.smbOut = new SmbFileOutputStream(filePath, false);
bf = new BufferedInputStream(file.getInputStream());
byte[] bt = new byte[8192];
int n = bf.read(bt);
while (n != -1) {
this.smbOut.write(bt, 0, n);
this.smbOut.flush();
n = bf.read(bt);
}
resultMap.put("fileName", fileName);
resultMap.put("filePath", filePath);
return resultMap;
} catch (SmbException e) {
throw new ExamException(e.getMessage());
} catch (MalformedURLException e) {
throw new ExamException(e.getMessage());
} catch (UnknownHostException e) {
throw new ExamException(e.getMessage());
} catch (IOException e) {
throw new ExamException(e.getMessage());
} finally {
try {
if (null != this.smbOut)
this.smbOut.close();
if (null != bf)
bf.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
public static boolean download(HttpServletResponse response, String url){
BufferedInputStream is = null;
BufferedOutputStream os = null;
try{
SmbFile file=new SmbFile(url);
SmbFileInputStream ins = new SmbFileInputStream(file);
String fileName=file.getName();
if(ins==null || response == null || StringUtils.isBlank(fileName)){
return false;
}
String mimeType = new MimetypesFileTypeMap().getContentType(fileName);
response.setContentType(mimeType + ";charset=UTF-8");
response.setHeader("Content-Length", String.valueOf(file.getContentLength()));
//转换中文字符串,防止出现乱码
String newFileName = new String(fileName.getBytes("gbk"), "ISO8859-1");
response.setHeader("Content-disposition", "attachment; filename=\"" + newFileName +"\"");
is = new BufferedInputStream(ins);
os = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[1024];
int bytesRead;
while (-1 != (bytesRead = is.read(buff, 0, buff.length))) {
os.write(buff, 0, bytesRead);
}
}catch(Exception e){
e.printStackTrace();
return false;
} finally{
try{
if(is != null){
is.close();
}
if(os != null){
os.close();
}
}catch(Exception e1){
}
}
return true;
}
// 5. 在main方法里面测试
public static void main(String[] args) {
String localFile = "C:\\Users\\Administrator\\Desktop\\hr_employee_info.sql"; // 本地要上传的文件
File file = new File(localFile);
SmbUtil smb = SmbUtil.getInstance();
smb.uploadFile(file);// 上传文件
String mimeType = new MimetypesFileTypeMap().getContentType("20180802111243_jdp_dict.sql");
System.out.println(mimeType);
}
}