今天写的struts上传图片的类!简单

调用的地方

<a onClick="javascript:window.open('jsp/uploadphoto.jsp','myfacewindow', 'height=100,width=440, top=350, left=400, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');" style="cursor:hand;"><span class="STYLE2">上传</span></a> 推荐最佳比例为:(50px*50px)


下面是上传的界面    uploadphoto.jsp

<%@page contentType="text/html; charset=GBK" import="java.util.HashMap"%>
<style type="text/css">
<!--
body {
 margin-left: 0px;
 margin-top: 0px;
 margin-right: 0px;
 margin-bottom: 0px;
}
.STYLE1 {
 font-size: 12px;
 color: #000000;
}
.STYLE2 {
 color: #006699;
 font-weight: bold;
}
-->
</style>
<html>
<title>上传图片</title>
<body>&nbsp;<br/>
&nbsp;<span class="STYLE2">商务客图片上传</span><br/>
&nbsp;<span class="STYLE1">请您选择您要上传的图片,图片推荐:50像素*50像素!使用.jpg .gif .bmp格式</span>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <form id="uploadphoto_f" name="uploadphoto_f" enctype="multipart/form-data" method="post" action="../uploadphoto.do">
  <tr>
    <td height="41">
 <input name="flag" type="hidden" value="myface" />
 <input name="loadpathto" type="hidden" value="/uploadcard/myface/" />
 &nbsp;<input name="uploadphoto" type="file" id="uploadphoto" size="40" />
  <input name="upload_b" type="submit" id="upload_b" value=" 上传 " /></td>
  </tr></form>
</table>
</body>
</html>
<%String msg=request.getAttribute("msg")==null?"":request.getAttribute("msg")+"";
  if(!msg.equals("")){
%>
<script>
opener.document.location.href="Userinfo.do";
window.close();
</script>
 
<%}%>

接着是FORM 类       uploadphotoForm

package com.onlysoft.txl.user;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: fishsoft</p>
 *
 * @author Danny
 * @version 1.0
 */
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;

public class uploadphotoForm extends ActionForm {
 protected FormFile uploadphoto;
 public FormFile getUploadphoto() {
     return uploadphoto;
 }

 public void setUploadphoto(FormFile theFile) {
     this.uploadphoto = theFile;
 }
 
 public ActionErrors validate(
     ActionMapping mapping,
     HttpServletRequest request) {
   ActionErrors errors = null;
   //has the maximum length been exceeded?
   Boolean maxLengthExceeded =
       (Boolean) request.getAttribute(
           MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);

   if ( (maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {
     errors = new ActionErrors();
     errors.add(
         ActionMessages.GLOBAL_MESSAGE,
         new ActionMessage("maxLengthExceeded"));
   }
   return errors;
 }
 
 

}

这个是action类     uploadphotoAction

  1 None.gif package  com.onlysoft.txl.user;
  2 None.gif
  3 ExpandedBlockStart.gifContractedBlock.gif /** */ /**
  4InBlock.gif * <p>Title: </p>
  5InBlock.gif *
  6InBlock.gif * <p>Description: </p>
  7InBlock.gif *
  8InBlock.gif * <p>Copyright: Copyright (c) 2006</p>
  9InBlock.gif *
 10InBlock.gif * <p>Company: fishsoft</p>
 11InBlock.gif *
 12InBlock.gif * @author Danny
 13InBlock.gif * @version 1.0
 14ExpandedBlockEnd.gif */

 15 None.gif import  javax.servlet.http.HttpServletRequest;
 16 None.gif import  javax.servlet.http.HttpServletResponse;
 17 None.gif import  org.apache.struts.action.ActionForm;
 18 None.gif import  org.apache.struts.action.ActionMapping;
 19 None.gif import  org.apache.struts.action.ActionForward;
 20 None.gif
 21 None.gif import  com.onlysoft.txl. * ;
 22 None.gif import  java.sql.ResultSet;
 23 None.gif import  java.util.Map;
 24 None.gif import  java.util.HashMap;
 25 None.gif import  java.util.ArrayList;
 26 None.gif import  java.util.List;
 27 None.gif import  com.onlysoft.txl.mail.client.SenderMail;
 28 None.gif import  org.apache.struts.Globals;
 29 None.gif import  org.apache.struts.action.ActionMessage;
 30 None.gif import  org.apache.struts.action.ActionMessages;
 31 None.gif import  org.apache.struts.upload.FormFile;
 32 None.gif import  java.io.InputStream;
 33 None.gif import  java.io.ByteArrayOutputStream;
 34 None.gif import  javax.servlet.http.HttpSession;
 35 None.gif import  java.io.FileOutputStream;
 36 None.gif import  java.io.OutputStream;
 37 None.gif
 38 ExpandedBlockStart.gifContractedBlock.gif public   class  uploadphotoAction  extends  BaseAction dot.gif {
 39InBlock.gif  public ActionForward execute(
 40InBlock.gif        ActionMapping mapping,
 41InBlock.gif        ActionForm form,
 42InBlock.gif        HttpServletRequest request,
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        HttpServletResponse response) throws Exception dot.gif{
 44InBlock.gif   init(request);
 45ExpandedSubBlockStart.gifContractedSubBlock.gif   if (!isLogin) dot.gif{
 46InBlock.gif        return mapping.findForward("relogin");
 47ExpandedSubBlockEnd.gif        }

 48InBlock.gif   String flag = request.getParameter("flag");
 49InBlock.gif   String loadpathto = request.getParameter("loadpathto");
 50InBlock.gif   String filePath = request.getRealPath("/"); //取当前系统路径
 51InBlock.gif   String pathphoto = loadpathto;
 52InBlock.gif
 53InBlock.gif
 54ExpandedSubBlockStart.gifContractedSubBlock.gif   if (form instanceof uploadphotoForm) dot.gif{//如果form是uploadsForm
 55InBlock.gif     String encoding = request.getCharacterEncoding();
 56InBlock.gif     if ((encoding != null&& (encoding.equalsIgnoreCase("utf-8")))
 57ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 58InBlock.gif            response.setContentType("text/html; charset=GBK");//如果没有指定编码,编码格式为GBk
 59ExpandedSubBlockEnd.gif        }

 60InBlock.gif    uploadphotoForm theForm = (uploadphotoForm ) form;
 61InBlock.gif    FormFile file = theForm.getUploadphoto();//取得上传的文件
 62ExpandedSubBlockStart.gifContractedSubBlock.gif    try dot.gif{
 63InBlock.gif      InputStream stream = file.getInputStream(); //把文件读入
 64InBlock.gif
 65InBlock.gif      ByteArrayOutputStream baos = new ByteArrayOutputStream();
 66InBlock.gif
 67ExpandedSubBlockStart.gifContractedSubBlock.gif      if (file.getFileSize() > (200 * 1024)) dot.gif{
 68InBlock.gif        request.setAttribute("message""文件请控制在200K以下!");
 69InBlock.gif        return mapping.findForward("ErrorInfo");
 70ExpandedSubBlockEnd.gif      }

 71InBlock.gif      String contentType = file.getContentType();
 72InBlock.gif      System.out.println("文件类别:=" + contentType);
 73InBlock.gif      String filebackstr = "";
 74ExpandedSubBlockStart.gifContractedSubBlock.gif      if (contentType.equals("image/pjpeg")) dot.gif{
 75InBlock.gif        filebackstr = ".jpg";
 76ExpandedSubBlockStart.gifContractedSubBlock.gif      }
 else if (contentType.equals("image/gif")) dot.gif{
 77InBlock.gif        filebackstr = ".gif";
 78ExpandedSubBlockStart.gifContractedSubBlock.gif      }
 else if (contentType.equals("image/bmp")) dot.gif{
 79InBlock.gif        filebackstr = ".bmp";
 80ExpandedSubBlockStart.gifContractedSubBlock.gif      }
 else dot.gif{
 81InBlock.gif        request.setAttribute("message""文件格式不正确!");
 82InBlock.gif        return mapping.findForward("ErrorInfo");
 83ExpandedSubBlockEnd.gif      }

 84InBlock.gif      OutputStream bos = new FileOutputStream(filePath + pathphoto +
 85InBlock.gif                                              userid + file.getFileName()); //建立一个上传文件的输出流
 86InBlock.gif      int bytesRead = 0;
 87InBlock.gif      byte[] buffer = new byte[8192];
 88ExpandedSubBlockStart.gifContractedSubBlock.gif      while ( (bytesRead = stream.read(buffer, 08192)) != -1dot.gif{
 89InBlock.gif        bos.write(buffer, 0, bytesRead); //将文件写入服务器
 90ExpandedSubBlockEnd.gif      }

 91InBlock.gif      bos.close();
 92InBlock.gif      stream.close();
 93ExpandedSubBlockEnd.gif    }

 94ExpandedSubBlockStart.gifContractedSubBlock.gif    catch(Exception e)dot.gif{
 95InBlock.gif     System.err.print(e);
 96ExpandedSubBlockEnd.gif    }

 97InBlock.gif     String sql="update sync4j_user set MYFACEPATH='" +pathphoto.replaceFirst("/",""+userid+file.getFileName()+"' where userid="+userid;
 98InBlock.gif    db.doSql(sql, db.DELETE_MODE, conn);
 99InBlock.gif
100ExpandedSubBlockStart.gifContractedSubBlock.gif   }
elsedot.gif{
101InBlock.gif        request.setAttribute("message""上传有误");
102InBlock.gif        return mapping.findForward("ErrorInfo");
103ExpandedSubBlockEnd.gif   }

104InBlock.gif
105InBlock.gif
106InBlock.gif
107InBlock.gif   request.setAttribute("msg""上传成功");
108InBlock.gif   return mapping.findForward("loadok");
109InBlock.gif
110ExpandedSubBlockEnd.gif  }

111ExpandedBlockEnd.gif}

112 None.gif

转载于:https://www.cnblogs.com/QDuck/archive/2006/05/27/410701.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值