gpt4 book ai didi

java - 无法使用 HttpClient POST 请求上传文件

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:27:14 25 4
gpt4 key购买 nike

我有一个 JSP 页面 (/Test/faces/pages/admin/csvImport.jsp),它有一个表单和一个输入文件类型文件 uploader 。单击“上传”按钮,表单将提交到自己的 URL/Test/faces/pages/admin/csvImport.jsp 并上传文件。我想自动上传这个文件,因此我使用 HttpClient API 来获取文件并提交表单。我正在尝试使用 HttpClient API 通过 POST 请求上传 CSV 文件,但不知何故文件没有上传。这是执行上传的代码片段:

        HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

HttpPost httpPost = new HttpPost("http://localhost:9088/Test/faces/pages/admin/csvImport.jsp");
httpPost.setHeader("Cookie", "JSESSIONID="+ sessionId);
String boundary = "---------------------------" + System.currentTimeMillis();

httpPost.setHeader("Content-Type", "multipart/form-data; boundary="+boundary);

File file = new File("C:\\root\\projects\\DataSources1.csv");
FileBody fileBody = new FileBody(file);
StringBody importBody = new StringBody("Import", ContentType.TEXT_PLAIN);
StringBody csvImportFormBody = new StringBody("csvImportForm", ContentType.TEXT_PLAIN);
StringBody uniqueTokenBody = new StringBody("j911", ContentType.TEXT_PLAIN);
StringBody uniqueViewBody = new StringBody("j911", ContentType.TEXT_PLAIN);

MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();

multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.setBoundary(boundary);
multipartEntity.addPart("uploadFileName", fileBody);
multipartEntity.addPart("csvimportForm:ImportButton", importBody);
multipartEntity.addPart("csvImportForm", csvImportFormBody);
multipartEntity.addPart("uniqueToken", uniqueTokenBody);
multipartEntity.addPart("uniqueView", uniqueViewBody);

httpPost.setEntity(multipartEntity.build());
HttpResponse response = httpClient.execute(httpPost,localContext);

我想在 java 代码中模拟的原始 HTML 表单是:

<form id="csvImportForm" method="post" action="/Test/faces/pages/admin/csvImport.jsp" enctype="multipart/form-data">
<tr>
<td width="25%" class="trayheader">
<!-- "File Name" (label) --> <span id="csvImportForm:fileNameTitleId">File Name:</span></td>
<td width="50%">
<!-- File Selection (input) --> <input type="file" name="uploadFileName" style="width: 90%"></td>
<td width="25%">
<!-- Import (button) --> <input type="submit" value="Import" name="csvImportForm:importButton" id="csvImportForm:importButton" onclick="javascript:hideForm();" class="commandExButton" /></td>
</tr>
<input type="hidden" name="csvImportForm" value="csvImportForm" /><input type="hidden" name="csvImportForm:_idcl" /><input type="hidden" name="clickedLogoutLink" />
</form>

任何人都可以给出任何提示,为什么文件没有被上传?当我执行代码时,它只是获取 jsp 页面,并没有上传文件。

比较 header 和 POST 参数后更新:比较 header 和参数。使用 FORM 是:

Request               POST /Test/faces/pages/admin/csvImport.jsp HTTP/1.1
Accept */*
Referer https://host1:30862/Test/faces/pages/admin/csvImport.jsp
Accept-Language en-GB
User-Agent Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
Content-Type multipart/form-data; boundary=---------------------------7e0212b90428
Accept-Encoding gzip, deflate
Host host1:30862
Content-Length 2398
DNT 1
Connection Keep-Alive
Cache-Control no-cache
Cookie ssoLang=en; dbihybrid_preferredLanguage=en; dbihybrid_locationDepartment=in.false; wt_eid=2143773288100110717; JSESSIONID=W9vmLJRZkf7u2Pevmd4QdSC2hUvliVepaVm-tFuz9m7O4lkWYPUw!1008091679; PA.MinLogLevel=30

和 HTTPClient 是:

Cookie: JSESSIONID=9C_m5OE8QUquA9sdj4T5eOyVS1yXC6yb0jdiVCuaZ_X-MjPQqEMw!1008091679
Content-Type: multipart/form-data; boundary=---------------------------1459867674444
Accept-Language: en-US,en;q=0.5
Accept: */*
Cache-Control: no-cache
Referrer: https://host1:30862/Test/faces/pages/admin/csvImport.jsp
Content-Length: 2423
Host: host1:30862
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.3.1 (java 1.5)
Accept-Encoding: gzip,deflate

带有 FORM 的 POST 参数:

Content-Disposition: form-data; name="csvImportForm" value="csvImportForm"
Content-Disposition: form-data; name="uniqueToken" value=""
Content-Disposition: form-data; name="uniqueView" value="j1154"
Content-Disposition: form-data; name="csvImportForm:importStatus" value=""
Content-Disposition: form-data; name="clipboard_l" value=""
Content-Disposition: form-data; name="clipboard_t" value=""
Content-Disposition: form-data; name="uploadFileName"; filename="C:\Users\Public\Documents\eclipse luna\workspace\WebScraper\properties\DataSources.csv" Content-Type: application/vnd.ms-excel
Content-Disposition: form-data; name="csvImportForm:importButton" value="Import"
Content-Disposition: form-data; name="javax.faces.ViewState" value="3514046226787645214:-6068281828613739341"

HttpClient 的 POST 参数:

Content-Disposition: form-data; name="uploadFileName"; filename="C:\Users\Public\Documents\eclipse luna\workspace\WebScraper\properties\DataSources.csv" Content-Type: application/vnd.ms-excel; charset=UTF-8
Content-Disposition: form-data; name="csvImportForm:importButton" value="Import"
Content-Disposition: form-data; name="csvImportForm" value="csvImportForm"
Content-Disposition: form-data; name="uniqueToken" value="j1228"
Content-Disposition: form-data; name="uniqueView" value="j1228"
Content-Disposition: form-data; name="javax.faces.ViewState" value="8959970480121731339:-7418251671370896640"
Content-Disposition: form-data; name="clipboard_l" value=""
Content-Disposition: form-data; name="clipboard_t" value=""
Content-Disposition: form-data; name="csvImportForm:importStatus" value=""

最佳答案

不确定这是您的问题所在,但 MultipartEntityBuilder 在内部处理 Content-Type header 和边界,因此您不必显式设置它们。也就是说,您可以安全地删除以下行

    String boundary = "---------------------------" + System.currentTimeMillis();

httpPost.setHeader("Content-Type", "multipart/form-data; boundary="+boundary);

否则它们可能会相互冲突。

由于您只是将边界设置为随机的东西,您可以让 MultipartEntityBuilder 为您完成。如果您不提供边界,这是默认行为。因此,您也可以删除

    multipartEntity.setBoundary(boundary);

关于java - 无法使用 HttpClient POST 请求上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36397871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com