java实现上传下载

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);
}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术学习分享

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值