JAVA上傳檔案(CSV)後轉成資料SHOW在網頁上
web.xml需有下面的FILTER才不會錯誤,Libraries需Import 2個jar檔,commons.io,commons.fileupload(apache的commons packages)
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
Xhtml程式碼,使用Primefaces fileUpload元件
<p:dialog id="uploadFileDialog" widgetVar="fileUploadForm" >
<p:fileUpload fileUploadListener="#{inPackingListCtrl.uploadFile}"
auto="true"
oncomplete="fileUploadForm.hide()"
update="dtb2"
allowTypes="/(\.|\/)(csv)$/"/>
</p:dialog>
java程式碼
public void uploadFile(FileUploadEvent event) {
System.out.println("upload File start!!! ");
String line;
String[] record;
try {
InputStream in = event.getFile().getInputstream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in), 65536);
line = reader.readLine();
System.out.println("line=" + line.toString());
int x = 0;
while ((line = reader.readLine()) != null) {
InPackingListInputBean _n = new InPackingListInputBean();
mainDataLists.add(x,_n);
record = line.split(",");
_n.setInvoiceNo(record[0]);
_n.setWaybillNo(record[1]);
_n.setSeq(Integer.parseInt(record[2]));
_n.setContainerNo(record[3]);
_n.setMaterialName(record[4]);
_n.setType(record[5]);
_n.setWeight(Integer.parseInt(record[6].toString()));
mainDataLists.set(x, _n);
x++;
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("upload file End !!! ");
}
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
Xhtml程式碼,使用Primefaces fileUpload元件
<p:dialog id="uploadFileDialog" widgetVar="fileUploadForm" >
<p:fileUpload fileUploadListener="#{inPackingListCtrl.uploadFile}"
auto="true"
oncomplete="fileUploadForm.hide()"
update="dtb2"
allowTypes="/(\.|\/)(csv)$/"/>
</p:dialog>
java程式碼
public void uploadFile(FileUploadEvent event) {
System.out.println("upload File start!!! ");
String line;
String[] record;
try {
InputStream in = event.getFile().getInputstream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in), 65536);
line = reader.readLine();
System.out.println("line=" + line.toString());
int x = 0;
while ((line = reader.readLine()) != null) {
InPackingListInputBean _n = new InPackingListInputBean();
mainDataLists.add(x,_n);
record = line.split(",");
_n.setInvoiceNo(record[0]);
_n.setWaybillNo(record[1]);
_n.setSeq(Integer.parseInt(record[2]));
_n.setContainerNo(record[3]);
_n.setMaterialName(record[4]);
_n.setType(record[5]);
_n.setWeight(Integer.parseInt(record[6].toString()));
mainDataLists.set(x, _n);
x++;
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("upload file End !!! ");
}
留言
張貼留言