主方法:
package com.ninemax.application.previews;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ninemax.application.util.FileTransfer;
import com.ninemax.application.util.JcifsUtil;
import com.ninemax.application.util.Util;
@SuppressWarnings("all")
public class FilePreviewsByOnline extends HttpServlet {
//序列化
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException {
//得到文件名称
String name = request.getParameter("name").trim().replace(",", " ");
try {
// 读取配置文件
String smb = Util.findProperties("smb.properties", "smb");
// 目标路径
String targetFile = request.getServletContext().getRealPath("/")
+ "upload\\temp";
// 从远程服务器下载pdf到系统服务器
File file = JcifsUtil.smbGet(smb + "/" + name, targetFile);
// 把文件从系统服务器下载到指定位置
FileTransfer.CopyFile(file, targetFile);
// 判断文件是否能找到
String json = "";
if(file.getName() != null){
json = "{\"fileName\":\""+file.getName()+"\"}";
}else{
System.out.println("========>文件未找到<========");
}
//返回响应
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
this.doPost(request, response);
}
}
三个工具类:
1.读取配置文件
package com.ninemax.application.util;
import java.io.InputStream;
import java.util.Properties;
/**
* 读取配置文件工具类
*
* @author Darker
*
*/
public class Util {
public static String findProperties(String propertiesPath,
String propertiesName) {
try {
// 读取配置文件工具类
Properties p = new Properties();
// 读取配置文件(与类在同一级,也可用/package/path/to/fileName)
InputStream in = Util.class.getResourceAsStream(propertiesPath);
// 加载配置文件
p.load(in);
// 得到配置文件属性
String val = p.getProperty(propertiesName);
// 关闭流
in.close();
return val;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
2.远程文件写入本地系统方法(smb)
package com.ninemax.application.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 远程文件写入本地系统
*
* @author Darker
*
*/
@SuppressWarnings("all")
public class JcifsUtil {
private static Log log = LogFactory.getLog(JcifsUtil.class);
public static File smbGet(String remoteUrl,String localDir){
remoteUrl = getUrls(remoteUrl);
InputStream in = null;
OutputStream out = null;
try {
// 读取远程(路径+文件名)
SmbFile smbFile = new SmbFile(remoteUrl);
// 得到远程文件名称
String fileName = smbFile.getName();
// 本地路径
String pathname = localDir+File.separator+fileName;
// 得到本地文件
File localFile = new File(pathname);
// 读取远程文件
in = new BufferedInputStream(new SmbFileInputStream(smbFile));
// 写入本地文件
out = new BufferedOutputStream(new FileOutputStream(localFile));
// 读取文件大小
byte []buffer = new byte[1024];
// 读取文件行数
int n = 0;
// 循环写入本地文件
while((n = in.read(buffer)) != -1){
out.write(buffer,0,n);
out.flush();
}
return localFile;
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static String getUrls(String url){
if(url!=null){
url = url.replace("\\", "/");
}
return url;
}
}
3.本地系统文件写入指定目录(可有可无.1,2已经将文件从远程读取到项目下面)
package com.ninemax.application.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* 本地系统文件写入指定目录
*
* @author Darker
*
*/
@SuppressWarnings("all")
public class FileTransfer {
/**
* 将文件从系统服务器移动到指定目录
*
* @param file
* 系统服务器文件
* @param filePath
* 系统服务器文件路徑
*/
public static void CopyFile(File file, String filePath) throws Exception {
// 从系统服务器中读文件
FileInputStream in = new FileInputStream(file);
// 将文件写入到指定位置
FileOutputStream out = new FileOutputStream("D:\\temp\\"
+ file.getName());
// 开始执行
byte[] data = new byte[2048];
// 读取文件的行数
int n = 0;
// 循环读取
while ((n = in.read(data)) > 0) {
out.write(data);
}
in.close();
out.close();
}
}
配置文件:
smb=smb\://administrator\:ninemax@10.215.4.16/ht301
页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Previews File Online</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<body>
<a onclick="load('FilePreviewsByOnline?name=CBT 3754-1995.pdf')" href="javascript:void(0)" target="blank">在线预览</a>
<div style="display:none">
<a id="File" href="upload\temp\CBT 3754-1995.pdf" target="blank">文件</a>
</div>
</body>
<script src="js/jquery-1.4.4.min.js" type="text/javascript"></script>
<script>
function load(path){
$.ajax({
url : path,
type : "post",
dataType : "json",
success :function(data){
//打开页面
document.getElementById("File").click();
},
error :function(xhr, status) {
$.dialog.alert('加载数据失败, 服务器出现异常!');
}
});
}
</script>
</html>