package com.zx.example.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zx.example.util.ConfigHelper;
@SuppressWarnings("serial")
public class FileAction extends ActionSupport {
private static final Log log=LogFactory.getLog(FileAction.class);
private File[] myFile; //上传的文件
public File[] getMyFile() {
return myFile;
}
public void setMyFile(File[] myFile) {
this.myFile = myFile;
}
private String[] myFileFileName; //上传的文件名
public String[] getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String[] myFileFileName) {
this.myFileFileName = myFileFileName;
}
private String[] myFileContentType; //上传的文件类型
public String[] getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String[] myFileContentType) {
this.myFileContentType = myFileContentType;
}
private String uploadDir; //上传的目录
public String getUploadDir() {
return uploadDir;
}
public void setUploadDir(String uploadDir) {
this.uploadDir = uploadDir;
}
public String fileUpload(){
String fileDir=ServletActionContext.getServletContext().getRealPath(uploadDir);
log.info("fileDir="+fileDir);
for(int i=0;i<myFile.length;i++)
{
log.info("myFileFileName["+i+"]="+myFileFileName[i]);
log.info("myFileContentType["+i+"]="+myFileContentType[i]);
int pot=myFileFileName[i].lastIndexOf(".");
String suffix=myFileFileName[i].substring(pot,myFileFileName[i].length());
log.info("suffix="+suffix);
String newFileName=createNewFileName()+i+suffix;
log.info("newFileName="+newFileName);
File dir=new File(fileDir);
if(!dir.exists())
{
dir.mkdir();
}
File newFile=new File(dir,newFileName);
copyFile(myFile[i], newFile);
}
return SUCCESS;
}
/**
* 将源文件内容copy到目标文件中
* @param src 源文件,页面上传的文件
* @param dst 目标文件,重新命名并保存到服务器上的新文件
* @throws Exception
*/
public void copyFile(File src,File dst){
InputStream is=null;
OutputStream os=null;
try{
is = new BufferedInputStream(new FileInputStream(src));
os = new BufferedOutputStream(new FileOutputStream(dst));
byte buffer[] = new byte[8192];
int len = 0;
while((len = is.read(buffer))!=-1)
{
os.write(buffer, 0, len);
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(is!=null)
{
is.close();
}
}catch(IOException e)
{
e.printStackTrace();
}
try{
if(os!=null)
{
os.close();
}
}catch(IOException e)
{
e.printStackTrace();
}
}
}
/**
* 利用系统当前时间生成新的文件名称
* @return
*/
public String createNewFileName(){
Calendar now=Calendar.getInstance();
String targetName=String.valueOf(now.get(now.YEAR))+String.valueOf(now.get(now.MONTH)+1);
targetName+=String.valueOf(now.get(now.DAY_OF_MONTH))+String.valueOf(now.get(now.HOUR_OF_DAY));
targetName+=String.valueOf(now.get(now.MINUTE))+String.valueOf(now.get(now.SECOND));
return targetName;
}
/**
* 文件下载
* @throws
* @throws IOException
*/
public void fileDownload(){
try{
ConfigHelper config=new ConfigHelper();
HttpServletResponse response=ServletActionContext.getResponse();
String uploadDir=config.getMessage("upload");
System.out.println("uploadDir="+uploadDir);
String fileName="局数据制作单模板.xls";
String template=config.getMessage("template_excel");
File templateFile=new File(template);
//将模板文件的内容拷贝到新文件中并保存到upload目录下
File dir=new File("uploadDir");
if(!dir.exists())
{
System.out.println("upload目录不存在");
dir.mkdir();
}
File newFile=new File(dir,fileName);
copy(templateFile,newFile);
Workbook wb=Workbook.getWorkbook(newFile);
WritableWorkbook book=Workbook.createWorkbook(newFile, wb);
WritableSheet sheet=book.getSheet(0);
//属性样式定义
WritableCellFormat itemFormat=new WritableCellFormat();
itemFormat.setAlignment(jxl.format.Alignment.CENTRE);
itemFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
itemFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
Label label=null;
label=new Label(1,0,"河南工业大学2009",itemFormat);
sheet.addCell(label);
label=new Label(4,0,"2009-03-16",itemFormat);
sheet.addCell(label);
label=new Label(8,0,"紧急",itemFormat);
sheet.addCell(label);
label=new Label(1,1,"彭怀义",itemFormat);
sheet.addCell(label);
label=new Label(4,1,"郑州市正信科技发展有限公司",itemFormat);
sheet.addCell(label);
label=new Label(8,1,"13526884003",itemFormat);
sheet.addCell(label);
label=new Label(11,1,"penghuaiyi@163.com",itemFormat);
sheet.addCell(label);
int j=3;
while(j<10)
{
for(int i=0;i<24;i++)
{
label=new Label(i,j,"love",itemFormat);
sheet.setRowView(j, 400);
sheet.addCell(label);
}
j++;
}
book.write();
book.close();
wb.close();
InputStream is=new BufferedInputStream(new FileInputStream(newFile));
OutputStream os=new BufferedOutputStream(response.getOutputStream());
fileName=new String(fileName.getBytes("GBK"),"ISO-8859-1");
response.reset();
response.setContentType("application/x-msdownload");
response.setContentLength((int)newFile.length());
response.setHeader("Content-Disposition", "attachment;filename="+fileName);
byte[] buffer=new byte[8192];
int len=0;
while((len=is.read(buffer))!=-1)
{
os.write(buffer, 0, len);
}
is.close();
os.close();
}catch(Exception e){
System.out.println("---->error="+e.getMessage());
e.printStackTrace();
}
}
/**
* 将templateFile的内容拷贝到newFile中
* @param templateFile
* @param newFile
*/
private void copy(File templateFile,File newFile)
{
try{
InputStream is=new FileInputStream(templateFile);
OutputStream os=new FileOutputStream(newFile);
byte[] buffer=new byte[1024*4];
int len=0;
while((len=is.read(buffer))!=-1)
{
os.write(buffer, 0, len);
}
is.close();
os.close();
}catch(IOException e){
e.printStackTrace();
}
}
}