67,514
社区成员
发帖
与我相关
我的任务
分享
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import org.apache.struts2.ServletActionContext;
import com.copote.businessManage.common.carManage.bean.CarInfo;
import com.opensymphony.xwork2.ActionSupport;
public class FileUpLoad extends ActionSupport {
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
// 接受依赖注入的属性
private String savePath;
// 接受依赖注入的方法
public void setSavePath(String value) {
this.savePath = value;
}
private String getSavePath() throws Exception {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setTitle(String title) {
this.title = title;
}
public void setUpload(File upload) {
this.upload = upload;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getTitle() {
return (this.title);
}
public File getUpload() {
return (this.upload);
}
public String getUploadContentType() {
return (this.uploadContentType);
}
public String getUploadFileName() {
return (this.uploadFileName);
}
//执行文件上传操作
@Override
public String execute() throws Exception {
System.out.println("开始上传单个文件-----------------------");
System.out.println(getSavePath());
System.out.println("==========" + getUploadFileName());
System.out.println("==========" + getUploadContentType());
System.out.println("==========" + getUpload());
// 以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
//将上传的Excel文件中的数据存储到数据库中
Workbook workbook = null;
Sheet sheet = null;
Cell cell = null;
System.out.println("读取上传文件");
try {
System.out.println("上传文件路径"+getSavePath() + "\\"
+ getUploadFileName());
workbook = Workbook.getWorkbook(
new File(getSavePath() + "\\"
+ getUploadFileName()));
System.out.println("读取文件完毕!");
} catch (Exception e) {
System.out.println("无法打开");
e.printStackTrace();
}
try {
sheet = workbook.getSheet(0);
} catch (Exception e) {
System.out.println();
e.printStackTrace();
}
int columnCount = sheet.getColumns();
int rowCount = sheet.getRows();
//***********在这里进行数据库操作**************//
List<CarInfo> carlist= new ArrayList<CarInfo>();
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < columnCount; j++) {
cell = sheet.getCell(j, i);
System.out.print(cell.getContents());
System.out.print(" ");
}
System.out.print("<br>");
}
workbook.close();
return SUCCESS;
}
public String input() {
return "input";
}
}