ファイルのインポート
5173 ワード
jsp:
Action:アップロードされたファイルがuploadFileの場合、ファイル名はuploadFileFileName、つまりファイルの後にFileNameを追加しなければ認識できません.
1:エクスポートjarパッケージの違い'2:private File uploadFile=nullをインポートします.private String uploadFileFileName=null; なぜ「=null」の値を付けないと3:String directory=「/upload/role」に伝わらないのか.String targetDirectory= ServletActionContext.getServletContext().getRealPath(directory); --》targetDirectory = D:\lxq\zhuanyi\jar+js\apache-tomcat-5.5.23\webapps\TinyBee\upload\role
<s:form action="import.action" method="post" enctype="multipart/form-data" theme="simple">
<table border="1">
<tr>
<td> </td>
</tr>
<tr>
<td>
<s:file name="uploadFile" label=" " size="80"/>
</td>
</tr>
<tr><td>
<s:submit value="submit"></s:submit>
<s:reset value="reset"></s:reset>
</td></tr>
</table>
</s:form>
Action:アップロードされたファイルがuploadFileの場合、ファイル名はuploadFileFileName、つまりファイルの後にFileNameを追加しなければ認識できません.
package excl;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import test.BaseAction;
import test.User;
public class Import extends BaseAction{
private static final String SUCCESS = "success";
private File uploadFile=null;
private String uploadFileFileName=null;
public String loadRoleFile() {
String directory="/upload/role";
String targetDirectory=
ServletActionContext.getServletContext().getRealPath(directory);
String file=targetDirectory+"\\"+uploadFileFileName;
//
File target = new File(file);
// ,
if(target.exists()){
target.delete();
}
System.out.println(uploadFile);
System.out.println(uploadFileFileName);
// file ,
try {
FileUtils.copyFile(uploadFile,target);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List roleList = new ArrayList();
try {
FileInputStream fi = new FileInputStream(target);
Workbook wb = new HSSFWorkbook(fi);
Sheet sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum()+1;
for(int i=1;i<rowNum;i++){
User user = new User();
Row row = sheet.getRow(i);
int cellNum = row.getLastCellNum();
for(int j=0;j<cellNum;j++){
Cell cell = row.getCell(j);
String cellValue=null;
switch(cell.getCellType()){
// excel , ,
case 0:cellValue=String .valueOf((int)cell.getNumericCellValue());break;
case 1 : cellValue = cell.getStringCellValue(); break;
case 2 : cellValue = String.valueOf(cell.getDateCellValue()); break;
case 3 : cellValue = ""; break;
case 4 : cellValue = String.valueOf(cell.getBooleanCellValue()); break;
case 5 : cellValue = String.valueOf(cell.getErrorCellValue()); break;
}
switch(j){//
case 0 : user.setId(Integer.parseInt(cellValue));break;
case 1 : user.setPassword(cellValue);break;
case 2 : user.setUsername(cellValue);break;
}
}
roleList.add(user);
}
} catch (IOException e) {
e.printStackTrace();
}
Session session = this.getHibernateTemplate().getSessionFactory().openSession();
Transaction tx = null;
try {
tx=null;
tx=session.beginTransaction();
if(roleList.size()>0){
int roleNum=roleList.size();
for(int i=0;i<roleNum;i++){
session.save(roleList.get(i));
}
tx.commit();
}
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
}finally{
session.close();
}
return SUCCESS;
}
public String getUploadFileFileName() {
return uploadFileFileName;
}
public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
}
1:エクスポートjarパッケージの違い'2:private File uploadFile=nullをインポートします.private String uploadFileFileName=null; なぜ「=null」の値を付けないと3:String directory=「/upload/role」に伝わらないのか.String targetDirectory= ServletActionContext.getServletContext().getRealPath(directory); --》targetDirectory = D:\lxq\zhuanyi\jar+js\apache-tomcat-5.5.23\webapps\TinyBee\upload\role