How can I programmatically upload a file to a website?

I need to upload a file to a server that only provides a jsf webpage with a file upload button (via http). I have to automate a process (executed as a standalone java process) that generates a file and uploads the file to the server. Actually, the server on which the file is to be uploaded does not provide FTP or SFTP. Is there any way to do this?

Thanks Richie

+3
source share
5 answers

When programmatically presenting a form created by JSF, you need to make sure that you consider the following 3 things:

  • Maintain an HTTP session (of course, if stateful JSF server side state is enabled on the website).
  • Send the name-value pair of the hidden field javax.faces.ViewState.
  • - , .

, , . "" . :

  • GET .
  • cookie JSESSIONID.
  • javax.faces.ViewState . (, , , ), submit. / j_id.
  • multipart/form-data POST.
  • cookie JSESSIONID ( null) .
  • - javax.faces.ViewState .
  • .

HTTP . Java SE API java.net.URLConnection , . , Apache HttpClient HTTP- cookie Jsoup HTML.

kickoff, , <form> ( CSS Jsoup):

String url = "http://localhost:8088/playground/test.xhtml";
String viewStateName = "javax.faces.ViewState";
String submitButtonValue = "Upload"; // Value of upload submit button.

HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, new BasicCookieStore());

HttpGet httpGet = new HttpGet(url);
HttpResponse getResponse = httpClient.execute(httpGet, httpContext);
Document document = Jsoup.parse(EntityUtils.toString(getResponse.getEntity()));
String viewStateValue = document.select("input[type=hidden][name=" + viewStateName + "]").val();
String uploadFieldName = document.select("input[type=file]").attr("name");
String submitButtonName = document.select("input[type=submit][value=" + submitButtonValue + "]").attr("name");

File file = new File("/path/to/file/you/want/to/upload.ext");
InputStream fileContent = new FileInputStream(file);
String fileContentType = "application/octet-stream"; // Or whatever specific.
String fileName = file.getName();

HttpPost httpPost = new HttpPost(url);
MultipartEntity entity = new MultipartEntity();
entity.addPart(uploadFieldName, new InputStreamBody(fileContent, fileContentType, fileName));
entity.addPart(viewStateName, new StringBody(viewStateValue));
entity.addPart(submitButtonName, new StringBody(submitButtonValue));
httpPost.setEntity(entity);
HttpResponse postResponse = httpClient.execute(httpPost, httpContext);
// ...
+7

HttpClient, , , , , , " HttpClient-File FileUpload".

, .

+1

, - POST . POST Java, . POST- Java

0

, HTML , . , :

<form action="/RequestURL">
<input type=file name=file1>
<input type=textbox name=value1>
</form>

, POST URL:

String data = URLEncoder.encode("value1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
data += "&" + URLEncoder.encode("file1", "UTF-8") + "=" + URLEncoder.encode(FileData, "UTF-8");

// Send data
URL url = new URL("http://servername.com/RequestURL");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
wr.close();

, , , , , POST . , .

0

You can try using HtmlUnit . It provides a very simple API to simulate browser actions. I have already used this approach for similar requirements. It is very easy. You must try.

0
source

All Articles