如何使用JSP/Servlet将文件上传到服务器?我试过这个:
<form action="upload" method="post">
<input type="text" name="description" />
<input type="file" name="file" />
<input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
但是,我只获取文件名,而不是文件内容.当我添加 enctype="multipart/form-data"到<form>,然后request.getParameter()返回null.
在研究期间,我偶然发现了Apache Common FileUpload.我试过这个:
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request); // This line is where it died.
Run Code Online (Sandbox Code Playgroud)
不幸的是,servlet抛出了一个没有明确消息和原因的异常.这是堆栈跟踪:
SEVERE: Servlet.service() for servlet UploadServlet threw exception
javax.servlet.ServletException: Servlet execution threw an exception
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:313)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用PrimeFaces上传文件,但fileUploadListener上传完成后不会调用该方法.
这是观点:
<h:form>
<p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
mode="advanced"
update="messages"
sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
<p:growl id="messages" showDetail="true"/>
</h:form>
Run Code Online (Sandbox Code Playgroud)
还有豆子:
@ManagedBean
@RequestScoped
public class FileUploadController {
public void handleFileUpload(FileUploadEvent event) {
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
Run Code Online (Sandbox Code Playgroud)
我已经在方法上放置了一个断点,但它从未调用过.当使用mode="simple"和时ajax="false",它已被调用,但我希望它在高级模式下工作.我正在使用Netbeans和Glassfish 3.1.
描述我通过一个多滤波处理上传文件的位置.我创建了一个WAR文件并部署在Weblogic 10.3.3服务器上并出现错误:
<Dec 8, 2011 5:37:07 PM IST> <Error> <HTTP> <BEA-101020> <[ServletContext@26087289[app:playground module:playground.war path:/playground spec-version:null]] Servlet failed with Exception
java.lang.NoSuchMethodError: org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(Lorg/apache/commons/fileupload/RequestContext;)Ljava/util/List;
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at net.balusc.webapp.MultipartFilter.parseRequest(MultipartFilter.java:169)
at net.balusc.webapp.MultipartFilter.doFilter(MultipartFilter.java:123)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.doIt(WebAppServletContext.java:3684)
Truncated. see log file for complete stacktrace
Run Code Online (Sandbox Code Playgroud)
然后我在Tomcat 7.0.11服务器上部署了相同的WAR文件,并且它已成功运行.这是如何引起的,如何在Weblogic上成功部署?
file-upload ×3
java ×2
servlets ×2
java-ee ×1
jsf ×1
jsf-2 ×1
jsp ×1
primefaces ×1
weblogic ×1