我想将图像从客户端上传到服务器。我已经设法使用诸如 primefaces 之类的小工具来做到这一点。但这一次,我想尝试自己做,我在网上找到了有关如何做的信息,但我有点卡住了。我目前的问题是浏览器告诉我我为上传创建的标签没有绑定。我想了解,为什么应用程序找不到它以及我应该如何修复它。
我将粘贴我到目前为止所做的一切,以便您帮助我找出错误:
1.将commons-fileupload-1.2.1.jar和commons-io-1.4.jar添加到lib文件夹(自动添加到classpath)
2.创建了一个xml,可以使标签库可用(这放在WEB-INF文件夹中)
<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd">
<namespace>http://corejsf.com</namespace>
<tag>
<tag-name>upload</tag-name>
<component>
<component-type>javax.faces.Input</component-type>
<renderer-type>com.corejsf.Upload</renderer-type>
</component>
</tag>
</facelet-taglib>
3.为标签的实现创建一个包,并放置在一个名为com.corejsf的新包中;
这是来源:
package com.corejsf;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.component.EditableValueHolder;
import javax.faces.component.UIComponent;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.FacesRenderer;
import javax.faces.render.Renderer;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
@FacesRenderer(componentFamily="javax.faces.Input",
rendererType="com.corejsf.Upload")
public class UploadRenderer extends Renderer {
public void encodeBegin(FacesContext context, UIComponent component)
throws IOException {
if (!component.isRendered()) return;
ResponseWriter writer = context.getResponseWriter();
String clientId = component.getClientId(context);
writer.startElement("input", component);
writer.writeAttribute("type", "file", "type");
writer.writeAttribute("name", clientId, "clientId");
writer.endElement("input");
writer.flush();
}
public void decode(FacesContext context, UIComponent component) {
ExternalContext external = context.getExternalContext();
HttpServletRequest request = (HttpServletRequest) external.getRequest();
String clientId = component.getClientId(context);
FileItem item = (FileItem) request.getAttribute(clientId);
Object newValue;
ValueExpression valueExpr = component.getValueExpression("value");
if (valueExpr != null) {
Class<?> valueType = valueExpr.getType(context.getELContext());
if (valueType == byte[].class) {
newValue = item.get();
}
else if (valueType == InputStream.class) {
try {
newValue = item.getInputStream();
} catch (IOException ex) {
throw new FacesException(ex);
}
}
else {
String encoding = request.getCharacterEncoding();
if (encoding != null)
try {
newValue = item.getString(encoding);
} catch (UnsupportedEncodingException ex) {
newValue = item.getString();
}
else
newValue = item.getString();
}
((EditableValueHolder) component).setSubmittedValue(newValue);
((EditableValueHolder) component).setValid(true);
}
Object target = component.getAttributes().get("target");
if (target != null) {
File file;
if (target instanceof File)
file = (File) target;
else {
ServletContext servletContext
= (ServletContext) external.getContext();
String realPath = servletContext.getRealPath(target.toString());
file = new File(realPath);
}
try { // ugh--write is declared with "throws Exception"
item.write(file);
} catch (Exception ex) {
throw new FacesException(ex);
}
}
}
}
4.然后我加了一个servlet过滤器,用来区分拦截请求,和自定义标签实现放在同一个包里
这是它的来源:
package com.corejsf;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadFilter implements Filter {
private int sizeThreshold = -1;
private String repositoryPath;
public void init(FilterConfig config) throws ServletException {
repositoryPath = config.getInitParameter(
"com.corejsf.UploadFilter.repositoryPath");
try {
String paramValue = config.getInitParameter(
"com.corejsf.UploadFilter.sizeThreshold");
if (paramValue != null)
sizeThreshold = Integer.parseInt(paramValue);
}
catch (NumberFormatException ex) {
ServletException servletEx = new ServletException();
servletEx.initCause(ex);
throw servletEx;
}
}
public void destroy() {
}
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain chain)
throws IOException, ServletException {
if (!(request instanceof HttpServletRequest)) {
chain.doFilter(request, response);
return;
}
HttpServletRequest httpRequest = (HttpServletRequest) request;
boolean isMultipartContent
= ServletFileUpload.isMultipartContent(httpRequest);
if (!isMultipartContent) {
chain.doFilter(request, response);
return;
}
DiskFileItemFactory factory = new DiskFileItemFactory();
if (sizeThreshold >= 0)
factory.setSizeThreshold(sizeThreshold);
if (repositoryPath != null)
factory.setRepository(new File(repositoryPath));
ServletFileUpload upload = new ServletFileUpload(factory);
try {
@SuppressWarnings("unchecked") List<FileItem> items
= (List<FileItem>) upload.parseRequest(httpRequest);
final Map<String, String[]> map = new HashMap<String, String[]>();
for (FileItem item : items) {
String str = item.getString();
if (item.isFormField())
map.put(item.getFieldName(), new String[] { str });
else
httpRequest.setAttribute(item.getFieldName(), item);
}
chain.doFilter(new
HttpServletRequestWrapper(httpRequest) {
public Map<String, String[]> getParameterMap() {
return map;
}
// busywork follows ... should have been part of the wrapper
public String[] getParameterValues(String name) {
Map<String, String[]> map = getParameterMap();
return (String[]) map.get(name);
}
public String getParameter(String name) {
String[] params = getParameterValues(name);
if (params == null) return null;
return params[0];
}
public Enumeration<String> getParameterNames() {
Map<String, String[]> map = getParameterMap();
return Collections.enumeration(map.keySet());
}
}, response);
} catch (FileUploadException ex) {
ServletException servletEx = new ServletException();
servletEx.initCause(ex);
throw servletEx;
}
}
}
5.然后我在web.xml中注册了过滤器。(我想使用注释,但我不知道怎么做,有人知道我怎么能用注释做到这一点吗?)还添加了 corejsf.taglib.xml
<!-- NEEDED FOR FILE UPLOAD -->
<filter>
<filter-name>Upload Filter</filter-name>
<filter-class>com.corejsf.UploadFilter</filter-class>
<init-param>
<param-name>sizeThreshold</param-name>
<param-value>1024</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Upload Filter</filter-name>
<url-pattern>/faces/upload/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>facelets.LIBRARIES</param-name>
<param-value>/WEB-INF/corejsf.taglib.xml</param-value>
</context-param>
6.在我的 WebContent 文件夹中,我创建了一个名为 upload(上传文件的目标)的子文件夹
7.在 jsf 页面中,我使用标签进行上传和提交,还使用托管 bean 方法创建文件名:
....
<corejsf:upload target="upload/#{placeAddController.prepareUniqueIdentifier}" />
....
<h:commandButton value="Dalje" style=" font-weight: bold; font-size:150%; action="/submittedImage" />
和 java managedbean:
@ManagedBean
@RequestScoped
public class PlaceAddControler {
…
public String prepareUniqueIdentifier() {
return UUID.randomUUID().toString()+"png";
}
我看到的错误是导航到添加新标签的页面时:
javax.faces.view.facelets.FaceletException: Error Parsing /oglas.xhtml: Error Traced[line: 183] The prefix "corejsf" for element "corejsf:upload" is not bound.
at com.sun.faces.facelets.compiler.SAXCompiler.doCompile(SAXCompiler.java:387)
at com.sun.faces.facelets.compiler.SAXCompiler.doMetadataCompile(SAXCompiler.java:370)
at com.sun.faces.facelets.compiler.Compiler.metadataCompile(Compiler.java:123)
at com.sun.faces.facelets.impl.DefaultFaceletFactory.createMetadataFacelet(DefaultFaceletFactory.java:353)
at com.sun.faces.facelets.impl.DefaultFaceletFactory.getMetadataFacelet(DefaultFaceletFactory.java:231)
at com.sun.faces.facelets.impl.DefaultFaceletFactory.getMetadataFacelet(DefaultFaceletFactory.java:164)
at com.sun.faces.application.view.ViewMetadataImpl.createMetadataView(ViewMetadataImpl.java:102)
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:239)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Thread.java:662)
-我错过了什么?
- 我还需要做什么才能将文件从客户端传输到上传文件夹内具有唯一文件名的服务器文件系统。
我的最终目标不是将此图像添加到数据库中,而是将它们留在文件系统中。
我希望有一个人可以帮助我。
更新
我将 xmlns 添加到我的页面并修复了异常:
xmlns:corejsf="http://corejsf.com"
但是为什么当我点击按钮时,文件没有上传???