UpDownloadClientUtil 工具类
本文获取的文件是从远程共享计算机获取得到的二进制流,如果不需要用到远程获取,可以忽略,运用自己的获取文件方式
/**
* jcifs.smb.SmbFile 导入jcifs.jar包
* Title:远程上传下载文件工具类
* Description: 采用smb技术实现两个功能
* Company:依万达
* @author nizg
* @date 2016-7-8 下午03:35:17
*/
public class UpDownloadClientUtil {
/**
* 上传文件功能
* 分布式上传指定共享机器
* ConfigUtil 可以通过spring 注解配置
* @return int 0:失败 1:成功
*/
public static int uploadToClient(HttpServletResponse repResponse,
String fileName) {
/*
* SmbFileInputStream in = new SmbFileInputStream(
* "smb://nzgUser:121212@172.16.25.136/recordfile/storage/2016-03-20/2015032017341907003O918676636668.wav"
* ); byte[] b = new byte[8192]; int n; while((n=in.read(b)) > 0 ) {
* System.out.write( b, 0, n ); }
*/
// smb://xxx:xxx@172.16.25.136/testDir/
// xxx:xxx是共享机器的用户名密码
// repResponse.reset();
if (fileName != null) {// 传入文件名不能为空!
String subFile = fileName.substring(0, 4) + "-"
+ fileName.substring(4, 6) + "-" + fileName.substring(6, 8);
//获取远程共享机子的信息 ConfigUtil是你获取配置文件的属性值 key---value
String host = ConfigUtil.get("host");
String user = ConfigUtil.get("user");
String password = ConfigUtil.get("password");
String dir = ConfigUtil.get("recorddir");// /recordfile/storage
String url = "smb://" + user + ":" + password + "@" + host + "/"
+ dir + "/" + subFile + "/";
//连接远程机子 获取文件数据 上传文件
String fileurl = url + fileName;
SmbFile sf = null;
SmbFileInputStream smbIn = null;
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
OutputStream toClient = null;
try {
sf = new SmbFile(fileurl);
smbIn = new SmbFileInputStream(sf);
bufferedInputStream = new BufferedInputStream(smbIn);
toClient = repResponse.getOutputStream();
bufferedOutputStream = new BufferedOutputStream(toClient);
byte[] data = new byte[8192];
int len = 0;
while ((len = bufferedInputStream.read(data) )!= -1) {
bufferedOutputStream.write(data,0, len);
}
return 1;
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (toClient != null) {
toClient.close();
}
if (bufferedOutputStream != null) {
bufferedOutputStream.close();
}
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
if (smbIn != null) {
smbIn.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}else {
System.out.println("传入文件名为空!");
}
return -1;
}
/**
* 下载文件到客户端
* @param fileName 下载路径
* @return response
*/
public static HttpServletResponse getAudio(HttpServletResponse response,String fileName){
if(fileName!=null){
//实现缓存于response
String subFile = fileName.substring(0, 4) + "-"
+ fileName.substring(4, 6) + "-" + fileName.substring(6, 8);
//获取远程共享机子的信息
String host = ConfigUtil.get("host");
String user = ConfigUtil.get("user");
String password = ConfigUtil.get("password");
String dir = ConfigUtil.get("recorddir");// /recordfile/storage/recordfile/RECORD_FILE_DIR/
String url = "smb://" + user + ":" + password + "@" + host + "/"
+ dir + "/" + subFile + "/";
//连接远程机子 获取文件数据 上传文件
String fileurl = url + fileName;
SmbFile sf = null;
SmbFileInputStream smbIn = null;
InputStream inputStream = null;
try {
sf = new SmbFile(fileurl);
smbIn = new SmbFileInputStream(sf);
inputStream = new BufferedInputStream(smbIn);
//FileOutputStream out = new FileOutputStream("recruitment/plugin/"+File.separator+fileName);// 文件的存放路径
response.reset();
// 设置response的Header
OutputStream toClient = response.getOutputStream();
response.setContentType("application/octet-stream");
byte[] data = new byte[1024];
int len = 0;
while ((len = inputStream.read(data) )!= -1) {
toClient.write(data,0,len);
//out.write(data,0,len);
}
toClient.close();
inputStream.close();
smbIn.close();
//out.close();
}catch (Exception e) {
e.printStackTrace();
}
}else{
System.out.println("传入的文件不可以空");
}
return response;
}
/**
* 保存文件到本地服务器
* @param fileName 文件名
* @param savePath 服务器路径
*/
public static void getAudiotoClient(String fileName,String savePath){
//String fileName = request.getParameter("audioID");
//System.out.println("文件名"+fileName);
if(fileName!=null){
//实现缓存于response
String subFile = fileName.substring(0, 4) + "-"
+ fileName.substring(4, 6) + "-" + fileName.substring(6, 8);
//获取远程共享机子的信息
String host = ConfigUtil.get("host");
String user = ConfigUtil.get("user");
String password = ConfigUtil.get("password");
String dir = ConfigUtil.get("recorddir");// /recordfile/storage/recordfile/RECORD_FILE_DIR/
String url = "smb://" + user + ":" + password + "@" + host + "/"
+ dir + "/" + subFile + "/";
//连接远程机子 获取文件数据 上传文件
String fileurl = url + fileName;
SmbFile sf = null;
SmbFileInputStream smbIn = null;
InputStream inputStream = null;
try {
sf = new SmbFile(fileurl);
smbIn = new SmbFileInputStream(sf);
inputStream = new BufferedInputStream(smbIn);
//String savePath = request.getSession().getServletContext().getRealPath("/upload");
System.out.println("路径"+savePath);
File file = new File(savePath);
//判断上传文件的保存目录是否存在
if (!file.exists() && !file.isDirectory()) {
System.out.println(savePath+"目录不存在,需要创建");
//创建目录
file.mkdir();
}
//消息提示
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
String filename = sf.getName();
FileOutputStream out = new FileOutputStream(savePath + "\\" + filename);
byte[] data = new byte[1024];
int len = 0;
while ((len = inputStream.read(data) )!= -1) {
out.write(data,0,len);
}
inputStream.close();
smbIn.close();
out.close();
}catch (Exception e) {
e.printStackTrace();
}
}else{
System.out.println("传入的文件不可以空");
}
}
远程获取文件的jar包下载:http://download.youkuaiyun.com/download/luo201227/7739823 如果不需要用到 无需下载
2、
在sturct action里 方式里:
注意:由于getOutputStream()与jsp 的getWriter() 两个方式只能用其中一种3、struct:public class WorkOrderAction extends BaseAction { //..... public String getAudio(){ //二进制流执行1 用二进制返回null 或者在jsp 加out.clear();等 因为printWriter 与outputStream二选一 String fileName = request.getParameter("audioID"); UpDownloadClientUtil.getAudio(response,fileName); //文件执行方式2 /*String path = request.getSession().getServletContext().getRealPath("/upload"); UpDownloadClientUtil.getAudiotoClient(callinggRecord.getCallingRecord(),path);*/ return null;//返回null 如果返回SUCCESS;的话 需要在jsp里面加载out.clear().等方法 } } }
<action name="getAudio" class="workorderAction" method="getAudio"></action>4、jsp显示:比如音频 图片也是一样<audio id="audioID" src="<%=basePath %>workorder/getAudio.action?audioID=${audioid}" controls="controls">Your browser does not support the audio tag.</audio>