我有一个多文件上传工作正常但在我保存数据库中的图像路径之前我想做一件事。我的数据库有五列保存不同的图像,我想在不同的变量中逐个获取图像路径,以便保存在我的数据库中的不同列中。
第1列=图像1 第2栏=图2 ....
这是代码:
xhtml页面
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{siteBean.fileUploadAction}" />
</h:form>
</h:body>
ManagedBean
public void fileUploadAction(FileUploadEvent event) {
try {
OutputStream out = null;
InputStream in = event.getFile().getInputstream();
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
FacesContext aFacesContext = FacesContext.getCurrentInstance();
ServletContext context = (ServletContext) aFacesContext.getExternalContext().getContext();
String realPath = context.getRealPath("/");
File folder = new File(realPath + "/img/sites/");
folder.mkdirs();
String file = realPath + "/img/sites/" + event.getFile().getFileName();
out = new FileOutputStream(file);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = in.read(bytes)) != -1)
{
out.write(bytes, 0, read);
}
out.flush();
}
catch (Throwable ex) {
ex.printStackTrace();
}
}
我使用的是Netbeans 7.4,primefaces 4.0,common io 2.4,common file upload 1.3和jsf 2.2。