PrimefacesまたはTomahawkを使用してファイルのアップロードを行う方法を知っていますが、Apache Commons FileUploadを使用してファイルのアップロードを行おうとしていますが、これまでのところ、少し障害があります。フォームを使用していても、フォームmultipart/form-data
を送信すると、コンテンツタイプはになりapplication/x-www-form-urlencoded
ます。これが私のコードです
<h:body>
<h:form enctype="multipart/form-data">
Upload File
<input type="file" name="file"/>
<p:commandButton value="Submit" action="#{viewBean.submit}"/>
</h:form>
</h:body>
これが私のViewBean
@ManagedBean
@ViewScoped
public class ViewBean implements Serializable {
public void submit() {
String url = "/FileUploadServlet";
FacesContext context = FacesContext.getCurrentInstance();
try {
String contentType = context.getExternalContext().getRequestContentType();
context.getExternalContext().dispatch(url);
} catch (Exception e) {
logger.log(Level.SEVERE, "Exception when calling Servlet", e);
} finally {
context.responseComplete();
}
}
}
したがって、上記のコンテンツタイプを印刷しようとすると、が表示されましたapplication/x-www-form-urlencoded
。に置くajax="false"
とp:commandButton
、submit()
メソッドは呼び出されませんが、取り出すとenctype="multipart/form-data"
(まだ保持しますajax="false"
)、submit()
呼び出されますが、マルチパートではないapplication/x-www-form-urlencoded
ため、apache commons fileuploadはマルチパートではないため、例外をスローします。私がしていることのように思えますが、私はマルチパートの要求を得ることができないようです。助けてください