服务器获取另一个服务器上的文件和上传文件到服务器
纯属工作笔记,写的不好,请看过的留下评论,多多指教!
获取服务器数据
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class PdfConvertUtil {
// 连接URL获取接口返回的数据
public static void pdfToHtml(String urls, String txtpath) {
HttpURLConnection httpurl = null;
InputStream in = null;
FileOutputStream fos = null;
try {
URL url = new URL(urls);
httpurl = (HttpURLConnection) url.openConnection();
// 设置超时
httpurl.setConnectTimeout(1000 * 5);
// 设置请求方式,默认是GET
httpurl.setRequestMethod("GET");
// 设置字符编码
httpurl.setRequestProperty("Charset", "UTF-8");
// 打开到此 URL引用的资源的通信链接
httpurl.connect();
in = httpurl.getInputStream();
fos = new FileOutputStream(new File(txtpath));
int len = 0;
byte[] b = new byte[1024];
while ((len = in.read(b)) != -1) {
fos.write(b, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} catch (org.w3c.dom.ls.LSException e) {
e.printStackTrace();
} catch (UnsupportedOperationException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (httpurl != null) {
httpurl.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
向服务器上传文件
/**
* 向服务器上传文件
* @param text
* @return
*/
public boolean setURL(String text,String urls) {
URL url = null;
HttpURLConnection httpUrlConnection = null;
OutputStreamWriter writer = null;
try {
url = new URL(urls);
httpUrlConnection = (HttpURLConnection) url.openConnection();
//设置超时
httpUrlConnection.setConnectTimeout(1000 * 10);
//请求方式
httpUrlConnection.setRequestMethod("POST");
//设置请求头和编码
httpUrlConnection.setRequestProperty("Content-Type",
"application/json;charset=UTF-8");
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setUseCaches(false);
httpUrlConnection.connect();
OutputStream out = httpUrlConnection.getOutputStream();
writer = new OutputStreamWriter(out, "utf-8");
writer.write(text);
writer.flush();
int code = httpUrlConnection.getResponseCode();
if(code == 200){
return true;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (httpUrlConnection != null) {
httpUrlConnection.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
`服务器端接收文件
@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白名单验证
public static void checkWhiteIp() {
HttpServletRequest request = ServletUtils.getRequest();
// 请求的地址
String IP = IpUtils.getIpAddr(request);
//System.out.println("############ip:"+IP);
if(!SpringUtils.getBean(HZBSConfig.class).containsIp(IP)) {
throw new CustomException("not in white ip list");
}
}
获取IP
public static String getIpAddr(HttpServletRequest request)
{
if (request == null)
{
return "unknown";
}
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
{
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
{
ip = request.getHeader("X-Forwarded-For");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
{
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
{
ip = request.getHeader("X-Real-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
{
ip = request.getRemoteAddr();
}
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
}
基本上整个接收文件和上传文件的流程就是这样的
版权声明:本文为weixin_43434961原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。