码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-11-13 20:26:30      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   sp   

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:123456@172.16.10.136/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 }

 

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

标签:des   style   blog   http   io   color   ar   os   sp   

原文地址:http://www.cnblogs.com/leorain/p/4095614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!