struts upload//创建upload文件夹

struts upload//创建upload文件夹
1.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html>
 <head>
  <title>JSP for upfileForm form</title>
 </head>
 <body>
  <html:form action="/upfile" enctype="multipart/form-data" >
   filedata : <html:file property="filedata"/><html:errors property="filedata"/><br/>
   filename : <html:text property="filename"/><html:errors property="filename"/><br/>
   <html:submit/><html:cancel/>
  </html:form>
 </body>
</html>
2.UploadfileForm

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.test.struts.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

/**
 * MyEclipse Struts
 * Creation date: 11-03-2008
 *
 * XDoclet definition:
 * @struts.form name="uploadfileForm"
 */
public class UploadfileForm extends ActionForm {
 /*
  * Generated fields
  */

 /** fileData property */
 private FormFile fileData;

 /** fileName property */
 private String fileName;

 /*
  * Generated Methods
  */

 /**
  * Method validate
  * @param mapping
  * @param request
  * @return ActionErrors
  */
 public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {
  // TODO Auto-generated method stub
  return null;
 }

 /**
  * Method reset
  * @param mapping
  * @param request
  */
 public void reset(ActionMapping mapping, HttpServletRequest request) {
  // TODO Auto-generated method stub
 }


 public FormFile getFileData() {
  return fileData;
 }

 public void setFileData(FormFile fileData) {
  this.fileData = fileData;
 }

 /**
  * Returns the fileName.
  * @return String
  */
 public String getFileName() {
  return fileName;
 }

 /**
  * Set the fileName.
  * @param fileName The fileName to set
  */
 public void setFileName(String fileName) {
  this.fileName = fileName;
 }
}

3.UploadfileAction

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.test.struts.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.test.struts.form.UploadfileForm;

/**
 * MyEclipse Struts
 * Creation date: 11-03-2008
 *
 * XDoclet definition:
 * @struts.action path="/uploadfile" name="uploadfileForm" input="/uploadfile.jsp" scope="request" validate="true"
 */
public class UploadfileAction extends Action {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  UploadfileForm f = (UploadfileForm) form;// TODO Auto-generated method stub
  String fileName=f.getFileName();
  FormFile fileData=f.getFileData();
  String realPath=this.getServlet().getServletContext().getRealPath("/");
  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
  String targetPath=realPath+"upload\\"+time;
  try {
   /////////////////create path file/////////////
   File upfile=new File(targetPath);
   if(!upfile.exists()){
    upfile.mkdir();
   }
   
   /////////////start upload//////////////
   InputStream in=fileData.getInputStream();
   OutputStream out=new FileOutputStream(targetPath+"\\"+fileData.getFileName());
   
   int readBytes=0;
   byte[] bytes=new byte[8192];
   while((readBytes = in.read(bytes, 0, 8192))!=-1){
    out.write(bytes,0,readBytes);
   }
   out.close();
   in.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return null;
 }
}


你可能感兴趣的:(struts upload//创建upload文件夹)