需求jar,jcifs-1.3.17.jar
jcifs
jcifs
1.3.17
windows下载
linuxg下载windows的文件
@WebServlet(asyncSupported = true,urlPatterns={"/servlet/DownloadServlet"})
public class DownloadServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
private final Logger logger= LoggerFactory.getLogger(this.getClass());
/**
* java web
* @param request
* @param response
*/
protected void doGet1(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
//获得请求文件名
String rqFilename = request.getParameter("filename");
System.out.println("参数中的文件名:"+rqFilename);
//logger.info("=======文件下载,文件名:"+filename);
String filename="";
try {
filename = new String(rqFilename.getBytes("ISO-8859-1"), "UTF-8");
//filename=URLEncoder.encode(filename,"UTF-8");
System.out.println("UTF-8文件名:"+filename);
//设置文件MIME类型
response.setContentType(getServletContext().getMimeType(filename));
//设置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename="+filename);//下载的文件是中文名,文件名需要经过url编码
//读取目标文件,通过response将目标文件写到客户端
//获取目标文件的绝对路径
//String fullFileName = getServletContext().getRealPath("/download/" + filename);
String fullFileName ="\\\\files\\目录\\"+filename;
//String fullFileName ="\\\\files\\目录\\文件名.cdr";
//fullFileName=new String(fullFileName.getBytes("ISO-8859-1"), "UTF-8");
System.out.println("路径:"+fullFileName);
//读取文件
InputStream in = new FileInputStream(fullFileName);
OutputStream out = response.getOutputStream();
System.out.println("OutputStream");
//写文件
byte[] buffer = new byte[1024];//可以自己 指定缓冲区的大小
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
System.out.println("while");
}
in.close();
out.close();
System.out.println("close");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("异常"+e.getMessage());
}
}
/**
* linux JAVA 访问windows共享文件夹
* 通过smb协议访问远程文件夹
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
SmbFileInputStream in = null;
OutputStream out = null;
String rqFilename=null,patchFileName=null,fileName=null;
rqFilename = request.getParameter("filename");
try {
patchFileName = new String(rqFilename.getBytes("ISO-8859-1"), "UTF-8");
System.out.println("参数文件名:" + patchFileName);
int index=patchFileName.lastIndexOf("/");
fileName=patchFileName.substring(index+1);
System.out.println("文件名:" + fileName);
//NETWORK_FOLDER = "smb://machineName/network_folder/";
//auth = new NtlmPasswordAuthentication(域, USER_NAME, PASSWORD);
String remotePhotoUrl="smb://10.10.1.15/"+patchFileName;
//有域
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", "srmuser@hooly.local", "passwrod.2020");
SmbFile smbFile = new SmbFile(remotePhotoUrl,auth);
//用户名:密码@windowIp/文件夹
//String remotePhotoUrl = "smb://test:Qwe.123456@10.10.10.205/"+patchFileName;
System.out.println("remotePhotoUrl="+remotePhotoUrl);
//SmbFile smbFile = new SmbFile(remotePhotoUrl);
//System.out.println("smbFile");
// 创建文件流
in = new SmbFileInputStream(smbFile);
System.out.println("in");
// 设置文件MIME类型
response.setContentType(getServletContext().getMimeType(fileName));
// 处理空格转为加号的问题
// response.setHeader("Content-Disposition", "attachment; fileName=" + fileName + ";filename*=utf-8''"
// + URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20"));
response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName,"UTF-8").replaceAll("\\+", "%20"));
System.out.println("Header");
out = response.getOutputStream();
System.out.println("out");
// 读取文件内容
byte[] buffer = new byte[1024];
int len = 0; // 读取长度
while ((len = in.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
}
out.flush(); //刷新缓冲的输出流
} catch (Exception e) {
String msg = "下载远程文件出错:" + e.getLocalizedMessage();
System.out.println(msg);
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (Exception e) {
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
前端代码
//多文件下载
for(var i=0;i<itemData.length;i++){
var filePath = zuanyi(itemData[i].filePath);
let url=serviceUrl+"?filename="+filePath;//注意要用let定义url
setTimeout(function() {
let frame = document.createElement("iframe");
frame.setAttribute("id", "iframe_" + i);
frame.style.display = "none"; // 防止影响页面
frame.style.height = 0;// 防止影响页面
frame.src = url;
//console.log(url);
document.body.appendChild(frame);
setTimeout(function() {
document.body.removeChild(frame);
}, 1000);
}, 100);
}
//单文件下载
function isIE () {
if (!!window.ActiveXObject || 'ActiveXObject' in window) {
return true
} else {
return false
}
},
function download () {
let url = '../../../static/cds/files/研究方案及团队情况表.docx'
if (this.isIE()) { // IE
window.open(url, '_blank')
} else {
let a = document.createElement('a') // 创建a标签
let e = document.createEvent('MouseEvents') // 创建鼠标事件对象
e.initEvent('click', false, false) // 初始化事件对象
a.href = url // 设置下载地址
a.download = 'xxxx表' // 设置下载文件名
a.dispatchEvent(e)
}
},