📄 fileupload.txt
字号:
public void fileUploaded(ValueChangeEvent event) {
UploadedFile file = (UploadedFile)event.getNewValue();
String fileName= null;
if (file != null && file.getLength() > 0) {
fileName = file.getFilename();
}else{
return ;
}
OperationBinding operationBinding = bindings.getOperationBinding("packRollList");
BufferedReader reader = null;
int row = 0;
StringBuffer rollIdList = new StringBuffer(100);
File tempFile = null;
try {
//创建文件,
tempFile = File.createTempFile(fileName,null);
//将
FileOutputStream fos = new FileOutputStream(tempFile);
InputStream is = file.getInputStream();
int data;
while((data=is.read())>0){
fos.write(data);
System.out.println(data+"\n");
}
fos.flush();
fos.close();
String boxId = null;
//读取文件内容
reader = new BufferedReader(new FileReader(tempFile));
String rollId = reader.readLine();
// int id ;
int rolls = 0;
int len = 0;
char c ;
int i;
while(rollId!=null){
row++;
len = rollId.length();
if(len >2){
c = rollId.charAt(0);
if(c=='-'){
StringBuffer sbBoxId = new StringBuffer(10);
i = 1;
c = rollId.charAt(i);
while(i<len-1&&c!=','){
if(c!='-'){
sbBoxId.append(c);
}
i++;
c = rollId.charAt(i);
}
boxId = sbBoxId.toString();
try{
Integer.parseInt(boxId);
}catch(NumberFormatException nfe){
operationBinding = bindings.getOperationBinding("Rollback");
operationBinding.execute();
return;
}
if(rollIdList.length()>1&&boxId!=null&&boxId.length()>0){
//将从文件读取的内容写到参数中packRollList对应的方法中
operationBinding.getParamsMap().put("rollIdList",rollIdList.substring(0,rollIdList.length()-1));
operationBinding.getParamsMap().put("boxId",boxId);
operationBinding.getParamsMap().put("rolls",Integer.valueOf(rolls));
operationBinding.execute();
if(operationBinding.getErrors().isEmpty()==false) {
operationBinding = bindings.getOperationBinding("Rollback");
operationBinding.execute();
return ;
}
}else{
operationBinding = bindings.getOperationBinding("Rollback");
operationBinding.execute();
return ;
}
rollIdList = new StringBuffer(100);
rolls = 0;
}else{
// id = Integer.parseInt(rollId.substring(1,9));
rollIdList.append("'").append(rollId.substring(0,len-2)).append("',");
rolls++;
}
}
rollId = reader.readLine();
System.out.println(rollId);
}
}catch(NumberFormatException nfe){
operationBinding = bindings.getOperationBinding("Rollback");
operationBinding.execute();
return ;
}catch(StringIndexOutOfBoundsException sioobe){
operationBinding = bindings.getOperationBinding("Rollback");
operationBinding.execute();
return ;
}
catch (IOException e) {
operationBinding = bindings.getOperationBinding("Rollback");
operationBinding.execute();
return ;
}finally{
try {
if(reader!=null)
reader.close();
if(tempFile!=null){
tempFile.delete();
}
} catch (IOException e) {
}
}
// try{
// operationBinding = bindings.getOperationBinding("Commit");
// operationBinding.execute();
// return ;
// }catch(JboException je){
// operationBinding = bindings.getOperationBinding("Rollback");
// operationBinding.execute();
// return ;
// }
}
public String done(){
Object result = fileNameInput.getValue();
if (result == null){
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage fm = new FacesMessage();
return null;
}else{
return "three";
}
} public void updateExecelToDB(ValueChangeEvent event)throws Exception{
Integer nameCol=null;
Integer priceCol=null;
String titName="名称";
String titPrice="单价";
UploadedFile file = (UploadedFile)event.getNewValue();
String fileName= null;
if (file != null && file.getLength() > 0) {
fileName = file.getFilename();
}else{
return ;
}
OperationBinding operationBinding = bindings.getOperationBinding("execelToDB");
//File tempFile=new File("d:/TEST.xls");
InputStream is = file.getInputStream();
jxl.Workbook rwb = Workbook.getWorkbook(is);
Sheet sheet = rwb.getSheet(0);
int columnum = sheet.getColumns();
int rownum = sheet.getRows();
//
for(int i=0;i<columnum;i++){
Cell colId = sheet.getCell(i, 0);
String strId = colId.getContents();
if(strId!=null&&strId.equals(titName)){
nameCol=i;
break;
}
}
for(int i=0;i<columnum;i++){
Cell colPrice = sheet.getCell(i, 0);
String strPrice = colPrice.getContents();
if(strPrice!=null&&strPrice.equals(titPrice)){
priceCol=i;
break;
}
}
if(nameCol==null&&priceCol==null){
ResourceBundle bundle = ResourceBundle.getBundle("webview.resources.UIResources");
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage message =
new FacesMessage(FacesMessage.SEVERITY_WARN, bundle.getString("srmain.srfileupload.error.filetype") , null);
context.addMessage(null,
message);
return ;
}
// Row row=iter
double price=0.00;
String name=null;
int printClo=2;
int lenRow=rownum-1;
int printPage=0;
int mod=lenRow%printClo;
if(mod==0){
printPage=(lenRow/printClo);
}else{
printPage=(lenRow/printClo)+1;
}
for(int i=0;i<printPage;i++){
ArrayList sb1=null;
ArrayList sb2=null;
sb1=new ArrayList();
sb2=new ArrayList();
//int row=0;
int rowCurr=(printClo*i)+1;
System.out.println("rowCurr="+rowCurr);
for(int x=rowCurr;x<=printClo*i+printClo;x++){
if(x>lenRow){
break;
}
Cell colId = sheet.getCell(nameCol, x);
name= colId.getContents();
if (name == null&&name.length()<1){
ResourceBundle bundle = ResourceBundle.getBundle("webview.resources.UIResources");
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage message =
new FacesMessage(FacesMessage.SEVERITY_WARN, bundle.getString("srmain.srfileupload.error.halfBaked") , null);
context.addMessage(null, message);
return ;
}
sb1.add(name);
Cell colPrice = sheet.getCell(priceCol, x);
String strPrice = colPrice.getContents();
if (strPrice == null&&strPrice.length()<1){
ResourceBundle bundle = ResourceBundle.getBundle("webview.resources.UIResources");
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage message =
new FacesMessage(FacesMessage.SEVERITY_WARN, bundle.getString("srmain.srfileupload.error.halfBaked") , null);
context.addMessage(null, message);
return ;
}
price=Double.parseDouble(strPrice)*1000;
sb2.add(price);
}
if(sb1!=null && sb2!=null){
operationBinding.getParamsMap().put("name",sb1);
operationBinding.getParamsMap().put("price",sb2);
operationBinding.execute();
if(operationBinding.getErrors().isEmpty()==false) {
operationBinding = bindings.getOperationBinding("Rollback");
operationBinding.execute();
return ;
}
}
}
operationBinding = bindings.getOperationBinding("Commit");
operationBinding.execute();
if(operationBinding.getErrors().isEmpty()){
ResourceBundle bundle = ResourceBundle.getBundle("webview.resources.UIResources");
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage message =
new FacesMessage(FacesMessage.SEVERITY_WARN, bundle.getString("srmain.srfileupload.success") , null);
context.addMessage(null, message);
return ;
}else{
ResourceBundle bundle = ResourceBundle.getBundle("webview.resources.UIResources");
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage message =
new FacesMessage(FacesMessage.SEVERITY_WARN, bundle.getString("srmain.srfileupload.error") , null);
context.addMessage(null, message);
return ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -