簡單紀要:java后台實現 往url上傳文件


將文件 post 到一個上傳地址上存儲:

  public static void main(String[] args) {
        HttpClient client = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://uploadurl");// 上傳地址
        FileBody fileBody = new FileBody(new File("d:\\test.mp4"));// 文件位置
        StringBody stringBody;
        try {
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("file", fileBody);
            httpPost.setEntity(entity);
            HttpResponse response = client.execute(httpPost);
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                // 上傳成功
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM