需求:
使用java通过下载连接下载数据并上传到其他服务器。
下载:
第一:下载网络文件:
/**
* 下载网络文件
* @throws MalformedURLException
*/
public void downloadNet(String neturl, String outurl) throws MalformedURLException {
// 下载网络文件
int byteread = 0;
if(neturl == null || "".equals(neturl)) {//测试用例
neturl = "https://gss3.bdstatic.com/7Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike272%2C5%2C5%2C272%2C90/sign=6f48f892e324b899ca31716a0f6f76f0/54fbb2fb43166d22083ac7064c2309f79152d2d7.jpg";
}
if(outurl == null || "".equals(outurl.trim())) {//测试用例
outurl = "d:/abc.jpg";
}
URL url = new URL(neturl);
try {
URLConnection conn = url.openConnection();//打开连接
InputStream inStream = conn.getInputStream();//获取输入流程
FileOutputStream fs = new FileOutputStream(outurl);//输出流
byte[] buffer = new byte[1204];//缓冲
while ((byteread = inStream.read(buffer)) != -1) {//判断是否有数据,并读取
fs.write(buffer, 0, byteread);//输出
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
第二:通过现有下载连接下载数据
数据抓包结果:我们可以直接在返回的数据中获取文件名称
public String Cookie = "JSESSIONID=abcAanHBnnsYu7tMUKl6w;";
/**
* 通过下载连接下载数据
* @param loadurl
*/
public String downloadNetLoad(String loadurl, String outurl, String faname) throws IOException {
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(loadurl);
//设置请求头
getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36");
//设置cookie,如果免登陆下载不需要设置
getMethod.setRequestHeader("Cookie", Cookie);//设置,原下载不验证的话,无需设置cookie
//默认的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
InputStream is = null;
OutputStream os = null;
String msg= null , str = null;
try{
//开始执行getMethod
int statusCode = httpClient.executeMethod(getMethod),byteread = 0;
if (statusCode != HttpStatus.SC_OK) System.err.println("Method failed:" + getMethod.getStatusLine());
else {
//流读取内容
//注意获取相关参数【具体可以查看实现类】
String [] rsparams =getMethod.getResponseHeader("content-disposition").toString().split(";")
for(String strs : rsparams) {
if(strs!= null && strs.indexOf("filename")!=-1) {//这里建议抓包获取你的文件名
faname = strs.split("=")[1];
break;
}
}
faname = faname.trim();
File file = new File(outurl+faname);
if(!file.exists()) { file.createNewFile(); }//文件判断
is = getMethod.getResponseBodyAsStream();
os = new FileOutputStream(file);
byte[] buffer = new byte[1204];//缓冲
while ((byteread = is.read(buffer)) != -1) {//判断是否有数据,并读取
os.write(buffer, 0, byteread);//输出
}
os.flush();
}
} catch(Exception e){//网络异常
}finally{
//释放连接
getMethod.releaseConnection();
if(is != null ) is.close();
if(os != null) os.close();
}
return msg;
}
//测试
String url = "http://localhost:8082/weaver/weaver.file.FileDownload?fileid=1983&requestid=-1";
String strmsg = downloadNetLoad(url, "d:"+File.separator, "load.txt");
上传:
同一个域
如果在同一个域,我们可以通过共享文件的方式使用smb(SMB(全称是Server Message Block)是一个协议名,它能被用于Web连接和客户端与服务器之间的信息沟通。)协议进行数据传输
// 共享文件夹所在服务器ip
private static String USER_DOMAIN = "10.10.32.12/weaver";
//访问用户
private static String USER_ACCOUNT = "test";
//访问密码
private static String USER_PWS = "test";
//共享文件夹地址
private static final String shareDirectory = "smb://10.10.32.12/weaver/dir";
//字节长度
private static final int byteLen = 1024;
/**
*
* @Title smbPut
* @Description 向共享目录上传文件
* @Param shareDirectory 共享目录
* @Param localFilePath 本地目录中的文件路径
* @date 2019-01-10 20:16
*/
public static void smbPut(String shareDirectory, File localFile) {
InputStream in = null;
OutputStream out = null;
try {
String fileName = localFile.getName();
// 域服务器验证
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(USER_DOMAIN, USER_ACCOUNT, USER_PWS);
SmbFile remoteFile = new SmbFile(shareDirectory + "/" + fileName, auth);
in = new BufferedInputStream(new FileInputStream(localFile));
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
byte[] buffer = new byte[byteLen];
while (in.read(buffer) != -1) {
out.write(buffer);
buffer = new byte[byteLen];
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务器有上传接口
上传抓包:
public String Cookie = "JSESSIONID=abcl9g_Yt1I7W6eBpzb6w;";
//测试
public void testUpload() {
String filepath = "d://load.txt";
Map<String, String> textMap = new HashMap<String, String>();
textMap.put("name", "text.txt");
textMap.put("isFirstUploadFile", "9");
Map<String, String> fileMap = new HashMap<String, String>();
//随便写入的参数
fileMap.put("userfile", filepath);
String upurl = "http://localhost:8082/api/workflow/reqform/docUpload?category=41,57,87&workflowid=219&listType=list&authStr=dmlld0NoYWluPTE1MjQwNTZ8bWFpbmlkPTE1MjQwNTZ8&authSignatureStr=16447a7f90b714a8508ce750dbf4283f&f_weaver_belongto_userid=1&f_weaver_belongto_usertype=0&wfTestStr= HTTP/1.1: ";
String ret = formUpload(upurl, textMap, fileMap);
}
public String formUpload(String urlStr, Map<String, String> textMap, Map<String, String> fileMap) {
HttpURLConnection conn = null;
// 换行符
final String newLine = "\r\n";
final String boundaryPrefix = "--";
String BOUNDARY = "----WebKitFormBoundaryCXD2k7zA9EPBHp0o"; // boundary就是request头和上传文件内容的分隔符
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(2000);
conn.setReadTimeout(3000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Cookie", Cookie);//session数据
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
// text
if (textMap != null) {
StringBuffer strBuf = new StringBuffer();
Iterator iter = textMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String inputName = (String) entry.getKey();
String inputValue = (String) entry.getValue();
if (inputValue == null) continue;
strBuf.append(newLine).append(boundaryPrefix).append(BOUNDARY).append(newLine);
strBuf.append("Content-Disposition: form-data; name=\""+ inputName + "\""+newLine+newLine);
strBuf.append(inputValue);
}
out.write(strBuf.toString().getBytes());
}
// file
if (fileMap != null) {
Iterator iter = fileMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String inputName = (String) entry.getKey(),
inputValue = (String) entry.getValue();
if (inputValue == null) continue;
File file = new File(inputValue);
String filename = file.getName();
String contentType = new MimetypesFileTypeMap().getContentType(file);
if (filename.endsWith(".png")) {contentType = "image/png"; }
if (contentType == null || contentType.equals("")) {
contentType = "application/octet-stream";
}
StringBuffer strBuf = new StringBuffer();
strBuf.append(newLine).append(boundaryPrefix).append(BOUNDARY).append(newLine);
strBuf.append("Content-Disposition: form-data; name=\""+ inputName + "\"; filename=\"" + filename+ "\""+newLine);
strBuf.append("Content-Type:" + contentType + newLine+newLine);
out.write(strBuf.toString().getBytes());
DataInputStream in = new DataInputStream( new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
}
}
byte[] endData = (boundaryPrefix + BOUNDARY + boundaryPrefix +newLine).getBytes();
out.write(endData);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return "";
}
附:
.net4.5下载连接:https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=30653
.fiddler下载连接(包含.Net4.5):
链接:https://pan.baidu.com/s/1ug1fziQ6zhoisxjsf0wAQw
提取码:3arb