两个服务器之间互相传输数据
服务器获取另一个服务器上的文件和上传文件到服务器
纯属工作笔记,写的不好,请看过的留下评论,多多指教!获取服务器数据
1 import java.io.File; 2 import java.io.FileOutputStream; 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.net.HttpURLConnection; 6 import java.net.URL; 7 8 public class PdfConvertUtil { 9 // 连接URL获取接口返回的数据 10 public static void pdfToHtml(String urls, String txtpath) { 11 HttpURLConnection httpurl = null; 12 InputStream in = null; 13 FileOutputStream fos = null; 14 try { 15 URL url = new URL(urls); 16 httpurl = (HttpURLConnection) url.openConnection(); 17 // 设置超时 18 httpurl.setConnectTimeout(1000 * 5); 19 // 设置请求方式,默认是GET 20 httpurl.setRequestMethod("GET"); 21 // 设置字符编码 22 httpurl.setRequestProperty("Charset", "UTF-8"); 23 // 打开到此 URL引用的资源的通信链接 24 httpurl.connect(); 25 in = httpurl.getInputStream(); 26 fos = new FileOutputStream(new File(txtpath)); 27 int len = 0; 28 byte[] b = new byte[1024]; 29 while ((len = in.read(b)) != -1) { 30 fos.write(b, 0, len); 31 } 32 } catch (IOException e) { 33 e.printStackTrace(); 34 } catch (org.w3c.dom.ls.LSException e) { 35 e.printStackTrace(); 36 } catch (UnsupportedOperationException e) { 37 e.printStackTrace(); 38 } catch (Exception e) { 39 e.printStackTrace(); 40 } finally { 41 try { 42 if (httpurl != null) { 43 httpurl.disconnect(); 44 } 45 } catch (Exception e) { 46 e.printStackTrace(); 47 } 48 try { 49 if (in != null) { 50 in.close(); 51 } 52 } catch (IOException e) { 53 e.printStackTrace(); 54 } 55 try { 56 if (fos != null) { 57 fos.close(); 58 } 59 } catch (IOException e) { 60 e.printStackTrace(); 61 } 62 } 63 } 64 }
向服务器上传文件
1 /** 2 * 向服务器上传文件 3 * @param text 4 * @return 5 */ 6 public boolean setURL(String text,String urls) { 7 URL url = null; 8 HttpURLConnection httpUrlConnection = null; 9 OutputStreamWriter writer = null; 10 try { 11 url = new URL(urls); 12 httpUrlConnection = (HttpURLConnection) url.openConnection(); 13 //设置超时 14 httpUrlConnection.setConnectTimeout(1000 * 10); 15 //请求方式 16 httpUrlConnection.setRequestMethod("POST"); 17 //设置请求头和编码 18 httpUrlConnection.setRequestProperty("Content-Type", 19 "application/json;charset=UTF-8"); 20 httpUrlConnection.setDoOutput(true); 21 httpUrlConnection.setUseCaches(false); 22 httpUrlConnection.connect(); 23 OutputStream out = httpUrlConnection.getOutputStream(); 24 writer = new OutputStreamWriter(out, "utf-8"); 25 writer.write(text); 26 writer.flush(); 27 int code = httpUrlConnection.getResponseCode(); 28 if(code == 200){ 29 return true; 30 } 31 32 } catch (MalformedURLException e) { 33 e.printStackTrace(); 34 } catch (ProtocolException e) { 35 e.printStackTrace(); 36 } 37 catch (IOException e) { 38 e.printStackTrace(); 39 } finally { 40 try { 41 if (writer != null) { 42 writer.close(); 43 } 44 } catch (IOException e) { 45 e.printStackTrace(); 46 } 47 try { 48 if (httpUrlConnection != null) { 49 httpUrlConnection.disconnect(); 50 } 51 } catch (Exception e) { 52 e.printStackTrace(); 53 } 54 } 55 return false; 56 }
`服务器端接收文件
@PostMapping("???")
public AjaxResult getReceiveContent(@RequestBody String text) {
//IP白名单验证
IpUtils.checkWhiteIp();
String attachmentid = null;
try {
//将接收到的数据存入附件表中
attachmentid = attachmentService.uploadTxt(text);
return AjaxResult.success(attachmentid);
} catch (IOException e) {
return AjaxResult.error();
}
服务器IP白名单验证
1 public static void checkWhiteIp() { 2 3 HttpServletRequest request = ServletUtils.getRequest(); 4 // 请求的地址 5 String IP = IpUtils.getIpAddr(request); 6 7 //System.out.println("############ip:"+IP); 8 9 if(!SpringUtils.getBean(HZBSConfig.class).containsIp(IP)) { 10 11 throw new CustomException("not in white ip list"); 12 13 } 14 }
获取IP
1 public static String getIpAddr(HttpServletRequest request) 2 { 3 if (request == null) 4 { 5 return "unknown"; 6 } 7 String ip = request.getHeader("x-forwarded-for"); 8 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) 9 { 10 ip = request.getHeader("Proxy-Client-IP"); 11 } 12 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) 13 { 14 ip = request.getHeader("X-Forwarded-For"); 15 } 16 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) 17 { 18 ip = request.getHeader("WL-Proxy-Client-IP"); 19 } 20 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) 21 { 22 ip = request.getHeader("X-Real-IP"); 23 } 24 25 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) 26 { 27 ip = request.getRemoteAddr(); 28 } 29 30 return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip; 31 }
基本上整个接收文件和上传文件的流程就是这样的
浙公网安备 33010602011771号