作者e23882 (呆呆要不要买降有)
看板java
标题[问题] 上传档案
时间Sat Dec 24 16:30:17 2016
大概就是我想用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://webptt.com/cn.aspx?n=bbs/java/M.1482568222.A.1FA.html
※ 编辑: e23882 (211.75.101.50), 12/24/2016 16:39:18