[問題] 上傳檔案

看板java作者 (呆呆要不要買降有)時間8年前 (2016/12/24 16:30), 8年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串5/5 (看更多)
大概就是我想用java去呼叫.net webform上傳的aspx 有google找幾個範例 版上有一篇類似的文解法是這個外國人寫的 http://0rz.tw/b0lWy 我自己是參考這篇,他寫的蠻清楚的 https://puremonkey2010.blogspot.tw/2012/07/java-httpclient-post-files.html 兩篇做法其實沒有太大的差異 主要差別就在於他呼叫的是php我呼叫的是.net 不知道為什麼動作都有送出去也有接到正常的回應 但資料都沒有傳過去 附上我的code 希望版上高手能給我一個思考的方向 感謝 http://49.158.107.110/Uploads/PostEx.java 這是我自己寫的webform上傳的網頁 http://49.158.107.110/WebSite1/loadfile.aspx import java.io.File; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; public class PostEx { public static void main(String[] args) throws Exception{ String ip = "49.158.107.110"; String cnt_type = "multipart/form-data"; String paramName = "FileUpload1"; String postFilePath = "D://12345.png"; String url = String.format("http://%s/WebSit1/loadfile.aspx", ip); HttpClient client = new DefaultHttpClient(); int statusCode = -1; System.out.printf("\t[Info] Upload file='%s'...\n", postFilePath); HttpPost post = new HttpPost( url ); MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE ); File paramValue = new File(postFilePath); // For File parameters entity.addPart( paramName, new FileBody((( File ) paramValue ), cnt_type )); post.setEntity( entity ); HttpResponse resp = client.execute(post); System.out.printf("\t[Info] Status Code=%d...\n", resp.getStatusLine().getStatusCode()); String response = EntityUtils.toString( resp.getEntity(), "UTF-8" ); System.out.printf("\t[Info] HTTP Response:\n%s\n", response); } } -- ▁▁▁ ╭╫╫╮ █ ██◣██◣█ █◣◢█ ◢█◣ █ _ █ █ █ █ █ █◣ █◥◤█ █ █ █◣ \ / ▋▋▋ █ ██ ◣█ █ █◥◣ █ █ ███ █◥◣ ﹀﹌﹀ ▋▋▋ █ █ █ █ █ ◥ █ █ █ █ █ ◥ /▉▄-( █ █ ███◤█ █ █ █ █ █ █ █ ψshadan -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 211.75.101.50 ※ 文章網址: https://www.ptt.cc/bbs/java/M.1482568222.A.1FA.html ※ 編輯: e23882 (211.75.101.50), 12/24/2016 16:39:18
文章代碼(AID): #1ONZ8U7w (java)
文章代碼(AID): #1ONZ8U7w (java)