English 中文(简体)
程序上将文件上传到 JSF Web 应用程序
原标题:Programmatically upload a file to JSF webapplication

我在Tomcat 7.0.25上有一个JSF2.0/primefaces网站应用程序。应用程序有一个简单的文件上传表格,如在主页showcase

<h:form enctype="multipart/form-data">
    <p:messages showDetail="true" />
    <p:fileUpload value="#{fileUploadController.file}" mode="simple" />
    <p:commandButton value="Submit" ajax="false" actionListener="#{fileUploadController.upload}" />
</h:form>
public void upload() throws IOException {
    FacesMessage msg = new FacesMessage("Succesful", this.file.getFileName() + " is uploaded.");
    FacesContext.getCurrentInstance().addMessage(null, msg);
    // do stuff with xml
}

I m trying to make a multipart/form-data POST request to te upload form using a stndalone java client. Sinc now I ve tried this and this but without success.

在这两种情况下,我都会收到 HTTP/1.1 200 OK 回复,我也会收到表格页面,好像POST没有发送一样。

有什么想法吗?

问题回答

在计划向JSF网络应用程序提交表格时,您需要确保考虑到以下三点:

  • Maintain the HTTP session (certainly if the JSF webapp has server side state saving turned on).
  • Send the name-value pair of the javax.faces.ViewState hidden input field.
  • Send the name-value pair of the submit button which is virtually to be pressed.

关于保持 HTTP 会话,在使用 < code> URLConnection 时,在 < /em > 之前增加以下一行 < em >,这是有史以来第一个 < code > URLConnection 创建过程。

CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

使用 HttpClient 时,请使用以下文字:

HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, new BasicCookieStore());
// ...
HttpResponse getResponse = httpClient.execute(someHttpMethod, httpContext);

发送 javax.faces.ViewState 隐藏输入字段中的名称=value 配对。 ViewState 硬性规定要保持 JSF 视图状态。 发送提交按钮中的名称=value 配对是强制规定的, 以便让 JSF 援引该动作。 如果没有自动生成的客户身份, 这是最容易的, 请给该客户及其父方一个固定的身份 。

See also:





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签