首页 > 代码库 > 上传文件到文件服务器并在web页面显示
上传文件到文件服务器并在web页面显示
1 hostIP=192.168.100.102 loginUser=leorain3 loginPwd=leorain4 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 xiezhonggui16 * */17 public class SmbUtil {18 19 /**20 * 把文件上传到局域网共享文件下21 * @param remoteUrl 共享电脑路径 如:smb//administrator:123456@172.16.10.136/smb22 * @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 remoteUrl57 * @param out58 */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 xiezhonggui19 *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页面显示
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。