在jsp页面中的关键
enctype="multipart/form-data">
----------------------------
请选择:
在ActionForm中的关键代码:
private FormFile uploadFile;
public FormFile getUploadFile() {
return uploadFile;
}
public void setUploadFile(FormFile uploadFile) {
this.uploadFile = uploadFile;
}
在Action中的关键代码:
FileLoadActionForm fileLoadActionForm = (FileLoadActionForm)form;
//对应的ActionForm
FormFile file = fileLoadActionForm.getUploadFile();
String filename = file.getFileName();
String filePath = servlet.getServletContext().getRealPath(
"/images/users/");
String dir = filePath + "//" + filename; //全路径名
try {
InputStream inputs = file.getInputStream();
OutputStream outputs = new FileOutputStream(dir);
int i;
byte[] buffer = new byte[8099];
while ((i = inputs.read(buffer, 0, 1024)) != -1) {
outputs.write(buffer, 0, i);
}
outputs.flush();
outputs.close();
inputs.close();
} catch (Exception ex) {
ex.printStackTrace();
}
-------------------------------------------------上传--------------------
public ActionForward down(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
System.out.println("--------------");
AgentProductForm agentProductForm = (AgentProductForm) form;// TODO Auto-generated method stub
ActionForward forward = new ActionForward();
ArrayList productList = new ArrayList();
AgentProduct agentProduct = new AgentProduct();
productList = agentProduct.queryProduct();
System.out.println("size:"+productList.size());
try{
String downFile = new StringBuffer().append("down_lang").append(".xls").toString();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;fileName="+response.encodeURL(downFile));
HSSFWorkbook wb = null;
HSSFSheet sheet = null;
HSSFFont font = null;
HSSFCell cell = null;
HSSFRow row = null;
HSSFCellStyle style = null;
wb = new HSSFWorkbook();
sheet = wb.createSheet("sheet 1");
font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setFont(font);
row = sheet.createRow(0);
sheet.addMergedRegion(new Region(0,(short)0,0,(short)5));
cell = row.createCell((short)0);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("产品数据");
row = sheet.createRow(1);
cell = row.createCell((short)0);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("产品编号");
cell = row.createCell((short)1);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("产品名称");
cell = row.createCell((short)2);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("产品地址");
cell = row.createCell((short)3);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("产品状态");
cell = row.createCell((short)4);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("新旧程度");
cell = row.createCell((short)5);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("创建日期");
Iterator it = productList.iterator();
int initRow = 2;
while(it.hasNext()){
AgentProductForm productForm = (AgentProductForm)it.next();
row = sheet.createRow(initRow);
cell = row.createCell((short)0);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(productForm.getProductId());
cell = row.createCell((short)1);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(productForm.getProductName());
cell = row.createCell((short)2);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(productForm.getAddress());
cell = row.createCell((short)3);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(productForm.getState());
cell = row.createCell((short)4);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(productForm.getNewLevel());
cell = row.createCell((short)5);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(productForm.getCreateDate());
initRow++;
}
OutputStream out = response.getOutputStream();
wb.write(out);
out.close();
return null;
}
catch(Exception e){
e.printStackTrace();
System.out.println("forward:"+forward);
return forward;
}
}
}
---------------------------上面是下载为EXCEL表格------------------------------
|
文件: |
Upload_demo.rar |
大小: |
2737KB |
下载: |
下载 | |
阅读(829) | 评论(0) | 转发(0) |