Rest Post示例(java服務端、python客戶端)

振宇要低調發表於2016-11-29

  前提:服務端是現成的,java、springMVC、resttemplate、jboss等;突然有個需要,要在windows上開發一個客戶端,作用是定期向服務端上傳檔案。想了想,如果客戶端寫一個java程式,勢必要求在windows上安裝java的執行環境,這樣比較麻煩。後來的解決思路是寫python的程式,然後打包成exe在windows上執行。以下是Rest Post示例的服務端、客戶端程式碼。

server端程式碼:

@RequestMapping(value = "/uploadScriptResult")
    public @ResponseBody OperateResult uploadScriptResult(MultipartFile files){
        //public @ResponseBody OperateResult uploadScriptResult(@RequestParam(value="files",required=false) MultipartFile files){
        OperateResult result = new OperateResult();
        try {
            log.info("uploadScriptResult start. ");
            result = uploadFile(files);
        } catch (Exception e) {
            log.error("uploadScriptResult error", e);
            result.setStatus(false);
            result.setMsg(e.getMessage());
        }
        return result;
    }
    
    private OperateResult uploadFile(MultipartFile file) {
        OperateResult result = new OperateResult();
        log.info("upload file start");
        String basePath = ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/") + Constants.REMOTE_SCRIPT_TEST_DIR + "execresult/" + getDate() + "/";
        File uploadPath =new File(basePath);    
        if(!uploadPath.exists())
            {       
            uploadPath.mkdir();    
        }
        String name = file.getOriginalFilename();
        name = name + "." + System.currentTimeMillis() + ".execresult.txt";
        String resourceURL = basePath + name;
        try {
            file.transferTo(new File(resourceURL));
            log.info("transfer file To: " + resourceURL + "success!");
            result.setMsg(name);
        } catch (Exception e1) {
            log.error("upload file error! resourceURL=" + resourceURL, e1);
            String msg = "upload file error resourceURL=" + resourceURL + " OriginalFilename: " + file.getOriginalFilename();
            result.setStatus(false);
            result.setMsg(msg);
        }
        return result;
    }
    
   private String getDate(){
        Date d = new Date();  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");  
        String dateNowStr = sdf.format(d);
        return dateNowStr;
    }


public class OperateResult {
    private Boolean status;
    private String msg;
    public OperateResult(){
        this.status = true;
        this.msg = "";
    }
    public Boolean getStatus() {
        return status;
    }
    public void setStatus(Boolean status) {
        this.status = status;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

 

client端程式碼:

import urllib
import urllib2
import requests
from poster.encode import multipart_encode
from urllib2 import Request, urlopen, URLError, HTTPError
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers

def uploadFile(filePath, url):
    register_openers()
    datagen, headers = multipart_encode({"files": open(filePath, "rb")})
    request = urllib2.Request(url, datagen, headers)
    response = urllib2.urlopen(request)
    print response.read()

if __name__ == "__main__":
    uploadFile("/home/testResult", "http://192.168.7.144:8888/starott_cloud_client/remotescripttest/uploadScriptResult");

 

相關文章