我试图在我的应用程序中包含一些功能,允许将CSV和XML格式的数据导入到我的数据库模式中的数据库表中。
以下是我目前编码的内容
import java.io.FileOutputStream;
import java.io.IOException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.apache.myfaces.custom.fileupload.UploadedFile;
import java.io.File;
@ManagedBean (name="importBean")
@SessionScoped
public class ImportBean {
private UploadedFile uploadedFile;
private String uploadedFileContents;
private String fileName;
private long fileSize;
private String fileContentType;
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
public String getUploadedFileContents() {
return uploadedFileContents;
}
public void setUploadedFileContents(String uploadedFileContents) {
this.uploadedFileContents = uploadedFileContents;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public long getFileSize() {
return fileSize;
}
public void setFileSize(long fileSize) {
this.fileSize = fileSize;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public void processFileUpload()
{
uploadedFileContents = null;
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
String path = ec.getRealPath("/");
File tempFile = null;
FileOutputStream fos = null;
try {
fileName = uploadedFile.getName();
fileName = FilenameUtils.getName(uploadedFile.getName());
fileSize = uploadedFile.getSize();
fileContentType = uploadedFile.getContentType();
tempFile = new File(path + fileName);
fos = new FileOutputStream(tempFile);
fos.close();
}
catch (IOException e)
{e.getMessage();}
}
}
JSP的代码如下所示
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>s16g26 File Upload</title>
</head>
<body>
<f:view>
<h:form enctype = "multipart/form-data">
<h:outputText value = "Select a File to Upload">
</h:outputText>
<h:inputFile value = "#{importBean.uploadedFile}">
</h:inputFile>
<h:commandButton type = "submit" value = "Upload"
action = "#{importBean.processFileUpload}">
</h:commandButton>
</h:form>
</f:view>
</body>
</html>
执行后,我在jsp代码行上出错。
错误状态无法将类org.apache.myfaces.shared.renderkit.html.util.HttpPartWrapper@39e0b28f
的{{1}}转换为接口org.apache.myfaces.shared.renderkit.html.util.HttpPartWrapper
我不知道如何解决此错误,并且在上传后,我不确定如何在数据库中动态创建表而不提及列名和列类型。任何帮助,将不胜感激。感谢。