package com.sais.inkaNet.common.displayImage;
import com.opensymphony.xwork2.ActionSupport;
import com.sais.inkaNet.base.util.FtpImageDisplay;
import com.sais.inkaNet.base.util.PropertiesUtil;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpServletResponse;
public class DisplayImageAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private static final Logger log = Logger.getLogger(DisplayImageAction.class);
/**
* <p>属性描述: [要访问的图片url]</p>
*/
private String imageUrl;
/**
* <p>属性描述: [ftp ip地址]</p>
*/
private String ip = PropertiesUtil.getSetting("ftp.ip", "");
/**
* <p>属性描述: [ftp密码]</p>
*/
private String password = PropertiesUtil.getSetting("ftp.password", "");
//---------ftp---------
/**
* <p>属性描述: [用户名]</p>
*/
private String username = PropertiesUtil.getSetting("ftp.username", "");
/**
* <p>方法描述: [获得ftp中的图片用于页面显示]</p>
*
* @return 返回结果的说明
*
* @throws Exception 抛出异常的原因
*/
@Override
public String execute() throws Exception {
log.info("-->获得ftp图片,imageUrl:"+imageUrl);
/**
1.+ 表示空格(在 URL 中不能使用 空格) %20
2./ 分隔目录和子目录 %2F
3.? 分隔实际的 URL 和参数 %3F
4.% 指定特殊字符 %25
5.# 表示书签 %23
6.& URL 中指定的参数间的分隔符 %26
7.@ %40
*/
imageUrl = imageUrl.contains("\\") ? imageUrl.replace("\\", "%2F") : imageUrl;
imageUrl = imageUrl.contains("/") ? imageUrl.replace("/", "%2F") : imageUrl;
password = password.contains("@")?password.replace("@", "%40"):password;
HttpServletResponse response = ServletActionContext.getResponse();
FtpImageDisplay.getFtpImage(username, password, ip, imageUrl, response);
return NONE;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
package com.sais.inkaNet.base.util; import org.apache.log4j.Logger; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; import javax.servlet.http.HttpServletResponse; public class FtpImageDisplay { /** * <p>属性描述: [声明日志]</p> */ private static final Logger log = Logger.getLogger(FtpImageDisplay.class); /** * <p>方法描述: [从ftp中获得图片流返给页面]</p> * * @param username 用户名 * @param password 密码 * @param ftpUrl ftp的url * @param fileUrl 文件路径 * @param response Response对象 */ public static void getFtpImage(String username, String password, String ftpUrl, String fileUrl, HttpServletResponse response) { InputStream isr = null; OutputStream out = null; URL u = null; try { if ((fileUrl != null) && !"".equals(fileUrl)) { response.setHeader("Content-Type", "image/png"); //response.setContentType("image/*"); // 设置返回的文件类型 out = response.getOutputStream(); StringBuffer sb = new StringBuffer("ftp://"); sb.append(username + ":"); sb.append(password + "@"); sb.append(ftpUrl + "/" + fileUrl); u = new URL(sb.toString()); URLConnection urlconn = u.openConnection(); isr = urlconn.getInputStream(); int len = urlconn.getContentLength(); int i = len; int c; while (((c = isr.read()) != -1) && (--i > 0)) { out.write(c); } log.info("获取ftp图片" + fileUrl); } else { log.warn("所要显示的图片路径为空"); } } catch (Exception e) { log.error(e.getMessage()); } finally { try { if (isr != null) { isr.close(); } if (out != null) { out.close(); } } catch (IOException e) { log.error(e.getMessage()); } } } }