❶ sap gui for java無法上傳excel
頁面jsp文件
<form name="add" action="/gxtWeb/lvjcontacts/manyContacts.action" method="post"enctype="multipart/form-data" onsubmit="return checkNull();">
<s:file name="upload"></s:file>
<input type="submit" value="導入信息" >
</form>
action處理:
public class ManyContactsAction extends ActionSupport {
private File upload;
private String uploadContentType;// 要上傳的文件的類型
private String uploadFileName;// 要上傳的文件
private ContactsManager manager;
private Contacts cbean;
private Persons pbean;
private Long gid;
private String result;
public String execute() throws Exception {
// 檢查後綴名是否符合條件,同時更改上傳文件的文件名
int filesize = this.getUploadFileName().length();
String fileEx = this.getUploadFileName().substring(
this.getUploadFileName().indexOf("."), filesize);
//獲取文件名
String fileName=uploadFileName.substring(0,uploadFileName.indexOf("."));
// 獲得上傳路徑
String realPath = ServletActionContext.getServletContext().getRealPath(
"/UploadFile/");
File saveFile=null;
if (upload != null) {
// 修改文件名,使上傳後不至於重復替代
// this.uploadFileName = new Date().getTime() + fileEx;
saveFile = new File(new File(realPath), uploadFileName);
if (!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
FileUtils.File(upload, saveFile);// 到這里,文件已上傳成功
// 下面進行判斷文件是否是rar文件,是就需要解壓
if (fileEx.equals(".rar")) {
System.out.println("saveFile:" + saveFile);//rar文件所在保存路徑
System.out.println("realPath:" + realPath);//解壓後保存路徑
// 定義解壓字元串,用於解壓上傳的rar文件,注意此處需要一個unrar.exe文件
String rarpath = ServletActionContext.getServletContext()
.getRealPath("/rarFile/UNRAR.exe x -t -o+ -p- \"");
String jieya = rarpath + saveFile + "\" \"" + realPath + "\"";
Process p1 = Runtime.getRuntime().exec(jieya);// 將傳輸的rar文件解壓
p1.waitFor();
p1.destroy();
FileUtils.deleteQuietly(saveFile);// 刪除rar文件
saveFile=new File(new File(realPath),fileName+".xls");
System.out.println("解壓後:"+saveFile);
ServletActionContext.getResponse().getWriter().println(
"success!!");
}
if (fileEx.equals(".xls") || fileEx.equals(".xlsx")) {
// 開始讀取文件了,獲得第一列手機號碼
Workbook persons = Workbook.getWorkbook(saveFile);// 獲得xls文件
Sheet sheet = persons.getSheet(0);// 獲得第一個工作簿
System.out.println("列數:" + sheet.getColumns());
int count = sheet.getRows();// 取得記錄數,count行
String cphone;
// 遍歷行,獲得列數據
for (int i = 0; i < count; i++) {
cphone = sheet.getCell(0, i).getContents();// 第一列的所有行
pbean = manager.getPerson(cphone);// 獲得該用戶,查詢別的信息
cbean = new Contacts();
Long contactsid = pbean.getId();
Long pid = 10002L;
cbean.setContactsid(contactsid);// 聯系人id
cbean.setPid(pid);// 用戶本身的PID
cbean.setCid("cid");// 關系的學校ID
cbean.setGid(gid);// 分組id
manager.addPerson(cbean);
this.contactsLog.writeLog("10002", "批量添加聯系人", "批量添加聯系人操作","");
System.out.println("添加成功!");
}
}
return SUCCESS;
} else {
return INPUT;
}
}
}
xml配置:
<action name="manyContacts" class="manyContactAction">
<result name="success" type="redirectAction">personInfo.action
</result>
<result name="input" type="redirectAction">showmangpersons.action
</result>
</action>