如何以编程方式将文件上传到网站? [英] How can i programmatically upload a file to a website?

查看:26
本文介绍了如何以编程方式将文件上传到网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将文件上传到服务器,该服务器仅公开带有文件上传按钮的 jsf 网页(通过 http).我必须自动化一个进程(作为 java 独立进程完成),它生成一个文件并将文件上传到服务器.遗憾的是,必须上传文件的服务器不提供 FTP 或 SFTP.有没有办法做到这一点?

I have to upload a file to a server which only exposes a jsf web page with file upload button (over http). I have to automate a process (done as java stand alone process) which generates a file and uploads the file to the server.Sadly the server to where the file has to be uploaded does not provide a FTP or SFTP. Is there a way to do this?

谢谢,里奇

推荐答案

当以编程方式提交 JSF 生成的表单时,您需要确保考虑以下 3 件事:

When programmatically submitting a JSF-generated form, you need to make sure that you take the following 3 things in account:

  1. 维护 HTTP 会话(当然,如果网站打开了 JSF 服务器端状态保存).
  2. 发送javax.faces.ViewState隐藏字段的名称-值对.
  3. 发送虚拟按钮的名称-值对.
  1. Maintain the HTTP session (certainly if website has JSF server side state saving turned on).
  2. Send the name-value pair of the javax.faces.ViewState hidden field.
  3. Send the name-value pair of the button which is virtually to be pressed.

否则可能根本不会调用该操作.对于残余,它与常规"形式没有什么不同.流程基本如下:

Otherwise the action will possibly not be invoked at all. For the remnant it's not different from "regular" forms. The flow is basically as follows:

  • 在带有表单的页面上发送 GET 请求.
  • 提取 JSESSIONID cookie.
  • 从响应中提取 javax.faces.ViewState 隐藏字段的值.如有必要(确定它是否具有动态生成的名称并因此可能更改每个请求),提取输入文件字段的名称和提交按钮.动态生成的 ID/名称可通过 j_id 前缀识别.
  • 准备一个 multipart/form-data POST 请求.
  • 针对该请求设置 JSESSIONID cookie(如果不是 null).
  • 设置javax.faces.ViewState隐藏字段和按钮的名称-值对.
  • 设置要上传的文件.
  • Send a GET request on the page with the form.
  • Extract the JSESSIONID cookie.
  • Extract the value of the javax.faces.ViewState hidden field from the response. If necessary (for sure if it has a dynamically generated name and thus possibly changes every request), extract the name of input file field and the submit buttonas well. Dynamically generated IDs/names are recognizeable by the j_id prefix.
  • Prepare a multipart/form-data POST request.
  • Set the JSESSIONID cookie (if not null) on that request.
  • Set the name-value pair of javax.faces.ViewState hidden field and the button.
  • Set the file to be uploaded.

您可以使用任何 HTTP 客户端库来执行该任务.标准 Java SE API 为此提供 java.net.URLConnection,即 相当低级.为了减少冗长的代码,您可以使用 Apache HttpClient 来执行 HTTP请求和管理 cookie 以及 Jsoup 从 HTML 中提取数据.

You can use any HTTP client library to perform the task. The standard Java SE API offers java.net.URLConnection for this, which is pretty low level. To end up with less verbose code, you could use Apache HttpClient to do the HTTP requests and manage the cookies and Jsoup to extract data from the HTML.

这是一个启动示例,假设页面只有一个

(否则您需要在 Jsoup 的 CSS 选择器中包含该表单的唯一标识符):

Here's a kickoff example, assuming that the page has only one <form> (otherwise you need to include an unique identifier of that form in Jsoup's CSS selectors):

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);
// ...

这篇关于如何以编程方式将文件上传到网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆