Chinaunix首页 | 论坛 | 博客
  • 博客访问: 859448
  • 博文数量: 221
  • 博客积分: 10033
  • 博客等级: 上将
  • 技术积分: 2325
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-06 22:00
文章分类

全部博文(221)

文章存档

2010年(8)

2009年(1)

2008年(69)

2007年(63)

2006年(80)

我的朋友

分类: Java

2007-11-03 16:14:58

1 upfile.jsp
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="" prefix="bean"%>
<%@ taglib uri="" prefix="html"%>
 

 
  JSP for UploadForm form
 
 
  enctype="multipart/form-data">
   
   filename :

   content :

   
   
  

 

2 uploadForm.java
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package cn.edu.nankai.tecpenguin.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-2007
 *
 * XDoclet definition:
 * @struts.form name="uploadForm"
 */
public class UploadForm extends ActionForm {
 protected FormFile File;
 /*
  * Generated fields
  */
 /** content property */
 private String content;
 /** filename property */
 private FormFile 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
 }
 /**
  * Returns the content.
  * @return String
  */
 public String getContent() {
  return content;
 }
 /**
  * Set the content.
  * @param content The content to set
  */
 public void setContent(String content) {
  this.content = content;
 }
 /**
  * Returns the filename.
  * @return String
  */
 public FormFile getFilename() {
  return filename;
 }
 /**
  * Set the filename.
  * @param filename The filename to set
  */
 public void setFilename(FormFile filename) {
  this.filename = filename;
 }
}
 
3 uploadaction
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package cn.edu.nankai.tecpenguin.struts.action;
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 cn.edu.nankai.tecpenguin.struts.form.UploadForm;
import java.io.*;
import javax.servlet.*;
import javax.servlet.jsp.*;
import javax.servlet.ServletContext;
import cn.edu.sdau.jiahaolin.time.ServerTime;
//import org.apache.struts.action.ActionForward;
//import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

/**
 * MyEclipse Struts
 * Creation date: 11-03-2007
 *
 * XDoclet definition:
 * @struts.action path="/upload" name="uploadForm" input="/upload.jsp" scope="request" validate="true"
 */
public class UploadAction 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) throws Exception{
  ServerTime time=new ServerTime();
  String path=time.date();
  
  UploadForm uploadForm = (UploadForm) form;// TODO Auto-generated method stub
  //FormFile file=uploadForm.getFilename();
  FormFile file=uploadForm.getFilename();
  //取当前系统路
  //FileOutputStream fileOutput;
         //System.out.println("****pi__ge****");
   String a=null;
    a=Long.toString(time.second())+".gif";
  //try{
   
  InputStream stream = file.getInputStream();
  String filePath = request.getRealPath("/");
  filePath=filePath+path;
  File addpath=new File(filePath);
  addpath.mkdir();
  
  //InputStream stream = file.getInputStream();
   ByteArrayOutputStream baos = new ByteArrayOutputStream();   
   OutputStream bos = new FileOutputStream(filePath +
      "/"+a);
  String contentType = file.getContentType();
   String filebackstr = "";
  
         // OutputStream bos = new FileOutputStream(filePath + file.getFileName()); //建立一个上传文件的输出流
   int bytesRead = 0;
    byte[] buffer = new byte[8192];  
    while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {   
    bos.write(buffer, 0, bytesRead);// 灏嗘枃浠跺啓鍏ユ湇鍔″櫒
    }  
    bos.close();
    stream.close();
   return mapping.findForward("success");
  //}catch(Exception e){
   //return mapping.findForward("error");
   //}
  }
}
  /**try{
   
   
   
  }catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
*/
  
  
4.ServerTime
package cn.edu.sdau.jiahaolin.time;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
public class ServerTime
{
    
 public long second()
    {
        Date date = new java.util.Date();
        long second = date.getTime() / 1000L;
        return second;
    }
     public static String date()throws Exception
    {
        String date = null ;
        try{Calendar cal = Calendar.getInstance();
         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
         date = formatter.format(cal.getTime());}
        catch(Exception e){}
         return date;
    }
     public static String datetime()throws Exception
    {    String date=null;
        try{Calendar cal = Calendar.getInstance();
        //SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
         date = formatter.format(cal.getTime());}
        catch(Exception e){}
        return date;
   }
    public String returndate()throws Exception
        {
    String returndate = date();
    return returndate;
        }
   }
  
  
  
 
 
阅读(2188) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~