上传图片(Eclipst 3.2 )

 ================================Action=========================

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

import java.io.*;
import java.util.Random;

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

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.yourcompany.struts.form.LoadForm;

public class LoadAction extends Action {

 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  LoadForm loadForm = (LoadForm) form;// TODO Auto-generated method stub
  HttpSession session = request.getSession();
  try {
   String name = readPhone(loadForm,session);
   return mapping.findForward("success");
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return null;
 }

 private String readPhone(LoadForm form, HttpSession session) throws IOException {
  String phoneName = "default.jpg";
  FormFile file = form.getPhotoName();
  if (!file.getFileName().equals("")) {
   String fileName = file.getFileName(); // 获取文件名
   Random rand = new Random();
   int s1 = rand.nextInt(9999999);
   int s2 = rand.nextInt(9999999);
   phoneName = " " + s1 + s2 + "_" + fileName;
   String path = session.getServletContext().getRealPath("/")
     + "photoDoc//" + phoneName;
   InputStream in_stream = null;
   OutputStream out_stream = null;
   try {
    in_stream = file.getInputStream();
    out_stream = new FileOutputStream(path);
     int bytesRead = 0;
                 byte[] buffer = new byte[8192];
                 while ((bytesRead = in_stream.read(buffer,0,8192))!= -1)
                 {
                      out_stream.write(buffer, 0, bytesRead);
                 } 
                 out_stream.close();
                 in_stream.close();
                 out_stream.flush();
                 out_stream.close();

   } catch (Exception e) {
    System.out.println(e.getMessage());
      phoneName="default.jpg";                
                  out_stream.close();
                  in_stream.close();  
                  out_stream.flush();
                  out_stream.close();                   
   }
  }
  file.destroy();
  return phoneName;
 }
}

===========================From======================

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.yourcompany.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: 02-02-2007
 *
 * XDoclet definition:
 * @struts.form name="loadForm"
 */
public class LoadForm extends ActionForm {
 /*
  * Generated Methods
  */
  private FormFile photoName ;
  

 public FormFile getPhotoName() {
   return photoName;
  }

  public void setPhotoName(FormFile photoName) {
   this.photoName = photoName;
  }

 /**
  * 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
 }
}

==============================JSP=======================

<%@ page language="java" pageEncoding="gb2312"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
 <html:base />

 <title>上传图片</title>

 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 <script type="text/javascript">
  function load()
  {
   var filevalue= document.form.photoName.value;
   if(filevalue!=null)
   {
    var filetext=document.form.photoName.value.substring(document.form.photoName.value.length-4,document.form.photoName.value.length)
   }
    filetext = filetext.toLowerCase(); 
            if(!(filetext=='.jpg'||filetext=='.gif'))   //照片格式
            {
                   document.form.photoName.focus();                 
                   alert("对不起,不正确的照片文件,必须为*.jpg或*.gif,*.jpeg文件!");      
                    return false;
            }  
            else
            {
              return true;
            }
    
  }
 </script>
</head>

<body>
 <html:form method="POST" action="/load.do" styleId="form" enctype="multipart/form-data" οnsubmit="return load(this)">
  <table width="200" border="1">
   <tbody>
    <tr>
     <td>
       <html:file property="photoName"/>
     </td>
    </tr>
    <tr>
     <td align="center">
      <html:submit value="上传" property="submit">
      </html:submit>
     </td>

    </tr>
  
   </tbody>
  </table>
 </html:form>
</body>
</html:html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值