I'm trying to include functionality in my application that would permit import of data from CSV and XML format into a database table in my database schema.
Below is what I've coded so far
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();}
}
}
The code for JSP is as shown below
<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>
Upon execution I get an error on the jsp code line .
The error states Cannot convert org.apache.myfaces.shared.renderkit.html.util.HttpPartWrapper@39e0b28f
of type class org.apache.myfaces.shared.renderkit.html.util.HttpPartWrapper
to interface org.apache.myfaces.custom.fileupload.UploadedFile
I have no idea how to tackle this error and also following the upload, I am unsure of how to dynamically create tables in the database without mentioning the column names and column types. Any help would be appreciated. Thanks.