最近遇到这样一个问题:
服务器A不能上外网,但功能需求让它能获取URL的图片,于是想到用同域的另外一台能上外网的服务器B上进行中转。方法是,在服务器B其上写个JSP或Servlet,A通过访问B上的JSP或Servlet,B以流的形式吐出,A获取流数据并生成图片。
以下是主要的实现代码
A上的请求Action主要代码
try {
String fileurl="http://www.iteye.com/upload/logo/user/166347/a62f68ef-ba87-3080-92ee-f95b3ff6189f.jpg?1257847065"; //图片外网地址
// String midUrl = "http://B's_ip:port/service/getImageFromUrl.jsp" + "?url=" +
fileurl;
String midUrl = "http://192.168.106.28:8092/getImage.do" + "?url=" + fileurl;
HttpURLConnection httpUrlCon = (HttpURLConnection)new URL(midUrl).openConnection();
httpUrlCon.setDoOutput(true);
httpUrlCon.setDoInput(true);
httpUrlCon.connect();
InputStream inputs = httpUrlCon.getInputStream();
BufferedImage image = ImageIO.read(inputs);
File tmpFile = new File("temp\\tmpFile"+new Date());
if(image!=null){
ImageIO.write(image, "jpg", tmpFile);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
B上的服务Serlvet类
public class GetImageServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
String url = request.getParameter("url");
System.out.println("url===="+url);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String filePath = "\tmpPath\\tmpFile"+sdf.format(new Date())+".jpg";
System.out.println("filePath===="+filePath);
try{
File tmpFile = new File(filePath);
FileUtils.copyURLToFile(new URL(url), tmpFile);
ServletOutputStream souts = response.getOutputStream();
response.setContentType("image/jpeg");
BufferedImage image = ImageIO.read(tmpFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(souts);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
encoder.encode(image, param);
ImageIO.write(image,"jpeg",response.getOutputStream());
souts.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
this.doGet(request, response);
}
}
分享到:
相关推荐
攻击者通过该漏洞可以读取服务器上的文件。在描述中,攻击者利用了URL编码技巧(%c0%ae等)来遍历目录,找到了管理员遗留的SSH私钥。 2. **SSH私钥滥用**: 管理员将SSH私钥留在了服务器上,这是极其不安全的做法...
其主要功能是接收来自客户端的请求,并根据请求内容,从远程服务器获取数据后缓存至本地。当下次再次请求相同的数据时,Squid 可直接从本地缓存中读取,从而提高数据传输效率及响应速度。目前版本支持 HTTP、FTP、...
1. **DNS在SSRF中的角色**:在SSRF攻击中,如果服务器执行DNS查询作为其一部分功能,攻击者可以通过构造特定的URL,诱导服务器进行恶意的DNS查询。例如,攻击者可能让服务器查询一个由他们控制的域名,从而获取...
创建“内网调外网接口服务”时,我们需要设置一个中间层,这个中间层负责接收内网的请求,转发到外网的API,然后将收到的响应回传给内网。以下是一些关键步骤: 1. **配置Spring MVC**:首先,我们需要在项目中引入...
SSL/TLS协议用于创建安全的网络连接,通过加密传输数据,防止中间人攻击和数据窃取。在Nginx服务器上配置SSL可以实现HTTPS服务,使网站能够处理敏感信息,如用户的登录凭证和在线交易。配置过程包括生成私钥、证书...
攻击者可以通过XXE漏洞获取服务器的敏感信息,例如读取服务器上的文件、进行端口扫描、甚至与其他服务器建立连接。这种攻击通常发生在应用程序处理不受信任的XML输入时,没有正确地限制或禁用外部实体的解析。 1. ...