一、項目需求
在b項目中前台上傳一個檔案,背景隻是負責擷取檔案,然後需要調用rest接口把檔案傳給另一個系統a(檔案服務系統),檔案伺服器系統會傳回給我需要的狀态,然後根據狀态判斷是否上傳成功。
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZwpmLkRGZwQzNkBTOycDOyQTOjJzM4IDOw8CX1EzLcRDM2EDMy8CXtVncvZ2LcRnbl1GajFGd0F2LcFGdhR2Lc12bj5SZslHdzRXayUjL3d3dvw1LcpDc0RHaiojIsJye.jpg)
二、檔案服務解決方案
1. 使用nsf 映射遠端磁盤目錄,将a的某個磁盤目錄映射到b的某個檔案夾,那麼,在b上傳檔案時,隻需要複制一份到這個映射目錄就可以了,系統會直接傳送的a伺服器的目錄裡,如果實在區域網路這個速度是可以忽略速度影響的,但是如果不是在區域網路可能會有傳輸速度影響。
2.利用伺服器rsync的同步工具。在b架設rsync的服務端,設定需要同步的檔案夾,在a設定rsync的用戶端,設定同步的來源伺服器和對應的檔案夾。當b的上傳檔案夾變動時,rsync會自動同步一份到a的客戶目錄。
4.如果檔案大小不是太大,b伺服器還可以利用各種中轉程式,例如先存到mysql,或者nosql的存儲裡,然後在a伺服器上自動去抓取下來。
5.當然你還可以使用靜态資源雲存儲,比如七牛,阿裡雲,使用它們的接口就能把資源上傳到它們的伺服器,接口十分簡單,費用非常便宜,比自建資源伺服器便宜很多,但是畢竟不是自己的。
這裡主要簡單實作第三種方案(使用語言java):
建立用戶端 用于上傳
httppostclient:
package com.itstyle.web;
import java.io.file;
import java.io.ioexception;
import org.apache.http.httpentity;
import org.apache.http.httpresponse;
import org.apache.http.httpstatus;
import org.apache.http.parseexception;
import org.apache.http.client.httpclient;
import org.apache.http.client.methods.httppost;
import org.apache.http.entity.mime.multipartentity;
import org.apache.http.entity.mime.content.filebody;
import org.apache.http.entity.mime.content.stringbody;
import org.apache.http.impl.client.defaulthttpclient;
import org.apache.http.util.entityutils;
/**
* 客服端
* 建立者 xxx
* 建立時間 2016年4月14日
* 科幫網 http://www.52itstyle.com
*
*/
public class httppostclient {
public void submitpost(string url, string filename, string filepath) {
httpclient httpclient = new defaulthttpclient();
try {
httppost httppost = new httppost(url);
filebody bin = new filebody(new file(filepath + file.separator + filename));
stringbody comment = new stringbody(filename);
multipartentity reqentity = new multipartentity();
reqentity.addpart("file", bin);// file為請求背景的file upload;屬性
reqentity.addpart("filename", comment);// filename為請求背景的普通參數;屬性
httppost.setentity(reqentity);
httpresponse response = httpclient.execute(httppost);
int statuscode = response.getstatusline().getstatuscode();
if (statuscode == httpstatus.sc_ok) {
system.out.println("伺服器正常響應.....");
httpentity resentity = response.getentity();
system.out.println(entityutils.tostring(resentity));// httpclient自帶的工具類讀取傳回資料
system.out.println(resentity.getcontent());
entityutils.consume(resentity);
}
} catch (parseexception e) {
e.printstacktrace();
} catch (ioexception e) {
} finally {
try {
httpclient.getconnectionmanager().shutdown();
} catch (exception ignore) {
}
}
/**
* 自定義檔案名稱 和路徑 win環境下測試
* @param args
*/
public static void main(string[] args) {
httppostclient httppostclient = new httppostclient();
httppostclient.submitpost("http://127.0.0.1:8080/acts_upload/receivedata","test.zip", "d://test");
}
複制代碼
建立服務端 用于接收
httppostserver:
import java.io.fileoutputstream;
import java.io.inputstream;
import java.io.printwriter;
import java.util.arraylist;
import java.util.iterator;
import java.util.list;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.apache.commons.fileupload.fileitem;
import org.apache.commons.fileupload.fileitemfactory;
import org.apache.commons.fileupload.disk.diskfileitemfactory;
import org.apache.commons.fileupload.servlet.servletfileupload;
* 服務端 接收檔案
public class httppostserver extends httpservlet {
private static final long serialversionuid = -1002826847460469784l;
@override
public void init() throws servletexception {
@suppresswarnings("unchecked")
public void doget(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
printwriter out = null;
response.setcontenttype("text/html;charset=utf-8");
string basepath = "d://test1";
fileitemfactory factory = new diskfileitemfactory();
servletfileupload upload = new servletfileupload(factory);
file directory = null;
list<fileitem> items = new arraylist<fileitem>();
string message = "";
items = upload.parserequest(request);
// 得到所有的檔案
iterator<fileitem> it = items.iterator();
while (it.hasnext()) {
fileitem fitem = (fileitem) it.next();
if(!fitem.isformfield()){
string name = fitem.getname();
if (name != null && !("".equals(name))) {
name = name.substring(name.lastindexof(file.separator) + 1);
directory = new file(basepath);
directory.mkdirs();
string filepath = (basepath) + file.separator + name;
inputstream is = fitem.getinputstream();
fileoutputstream fos = new fileoutputstream(filepath);
byte[] buffer = new byte[1024];
while (is.read(buffer) > 0) {
fos.write(buffer, 0, buffer.length);
}
fos.flush();
fos.close();
message = "{success:true, msg:'接收成功'}";
} catch (exception e) {
message = "{success:false, msg:'讀取http請求屬性值出錯!'}";
}finally{
out = response.getwriter();
out.print(message);
out.close();
public void dopost(httpservletrequest request, httpservletresponse response)
doget(request, response);
所需要jar包
commons-codec-1.6.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.1.1.jar
fluent-hc-4.2.jar
httpclient-4.2.jar
httpclient-cache-4.2.jar
httpcore-4.2.jar
httpmime-4.2.jar
密碼:
56d8