上传文件到文件服务器并在web页面显示

1 hostIP=192.168.100.10
2 loginUser=leorain
3 loginPwd=leorain
4 imagePath=images
 1 package com.usi.wxcm.common.util;
 2 
 3 
 4 import java.io.BufferedInputStream;
 5 import java.io.BufferedOutputStream;
 6 import java.io.File;
 7 import java.io.FileInputStream;
 8 import java.io.IOException;
 9 import java.io.InputStream;
10 import java.io.OutputStream;
11 import jcifs.smb.SmbFile;
12 import jcifs.smb.SmbFileInputStream;
13 import jcifs.smb.SmbFileOutputStream;
14 /**
15  * @author xiezhonggui
16  * */
17 public class SmbUtil {
18     
19     /**
20      * 把文件上传到局域网共享文件下
21      * @param remoteUrl 共享电脑路径 如:smb//administrator:[email protected]/smb
22      * @param localFile File对象
23      * @param fileName 保存的文件名
24      */
25     public static String smbPut(String remoteUrl, File localFile, String fileName) throws IOException{
26         InputStream in = null;
27         OutputStream out = null;
28         String remoteFileName = null;
29           try {
30                // String fileName = localFile.getName();
31                 SmbFile remoteFile = new SmbFile(remoteUrl+File.separator+fileName);
32                 in = new BufferedInputStream(new FileInputStream(localFile));
33                 out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
34                 byte []buffer = new byte[1024];
35                 while((in.read(buffer)) != -1){
36                     out.write(buffer);
37                     buffer = new byte[1024];
38                 }
39                 remoteFileName = remoteFile.getName();
40             } catch (Exception e) {
41                 e.printStackTrace();
42             }finally{
43                 try {
44                     out.close();
45                     in.close();
46                 } catch (IOException e) {
47                     e.printStackTrace();
48                 }
49                
50             }
51          return remoteFileName;
52     }
53     
54        /**
55         * 从文件服务器中读取图片文件输出到输出流中
56      * @param remoteUrl
57      * @param out
58      */
59     public static void smbGet(String remoteUrl,OutputStream out){
60             InputStream in = null;
61             try {
62                 SmbFile smbFile = new SmbFile(remoteUrl);
63                 in = new BufferedInputStream(new SmbFileInputStream(smbFile));
64                 byte []buffer = new byte[1024];
65                 while((in.read(buffer)) != -1){
66                     out.write(buffer);
67                     buffer = new byte[1024];
68                 }
69             } catch (Exception e) {
70                 e.printStackTrace();
71             }finally{
72                 try {
73                     out.flush();
74                     out.close();
75                     in.close();
76                 } catch (IOException e) {
77                     e.printStackTrace();
78                 }
79             }
80         }
81 }
 1 /**
 2  * 
 3  */
 4 
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 import java.util.Properties;
 8 
 9 import javax.servlet.ServletOutputStream;
10 import javax.servlet.http.HttpServletResponse;
11 
12 import org.apache.struts2.ServletActionContext;
13 
14 import com.opensymphony.xwork2.ActionSupport;
15 import com.usi.wxcm.common.util.SmbUtil;
16 
17 /**
18  * @author xiezhonggui
19  *
20  */
21 public class ImageAction extends ActionSupport {
22     /**
23      * 
24      */
25     private static final long serialVersionUID = -6038904337360637857L;
26     private String remoteUrl;
27     public ImageAction(){
28         remoteUrl = loadProperty();
29     }
30     public String viewImages() throws IOException {
31             HttpServletResponse response = null;
32             ServletOutputStream out = null;
33             response = ServletActionContext.getResponse();
34             response.setContentType("multipart/form-data");
35             out = response.getOutputStream();
38             SmbUtil.smbGet(remoteUrl+"/Desert.jpg", out);
39             return null;
40     }
41     
42     /**
43      * 加载配置文件
44      * 
45      * */
46     private String loadProperty(){
47         String remoteUrl = null;
48         Properties prop = new Properties();
49         InputStream in = ImageAction.class.getResourceAsStream("/smbConfig.properties");   
50         try {
51             prop.load(in);
52             String hostIP = prop.getProperty("hostIP").trim();   
53             String loginUser = prop.getProperty("loginUser").trim();
54             String loginPwd = prop.getProperty("loginPwd").trim();
55             String imagePath = prop.getProperty("imagePath").trim();
56             remoteUrl = "smb://"+loginUser+":"+loginPwd+"@"+hostIP+"/"+imagePath;
57         } catch (IOException e) {   
58             e.printStackTrace();   
59         }  
60         return remoteUrl;
61     }
62 
63     public String getRemoteUrl() {
64         return remoteUrl;
65     }
66 
67     public void setRemoteUrl(String remoteUrl) {
68         this.remoteUrl = remoteUrl;
69     };
70 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。