首页 新闻 搜索 专区 学院

用 MyEclipse6.0 编译工具写了一个 struts 上传图片的方法为什么上传不成功

0
悬赏园豆:20 [已关闭问题]

 

// 第一个类是写了一个 uploadphotoAction  

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

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

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.feng.form.*;

public class uploadphotoAction extends BaseAction   // 应该是继承这个BaseAction  为什么我的编译工具没有继承自 它的那个 BaseAction类 我用的是MyEclipse 6.0编写的类

public class uploadphotoAction extends Action {     

 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)throws Exception {
  // TODO Auto-generated method stub
  init(request);                                         // 这块init(request)方法也在报错 不知道是什么原因
    if (!isLogin) {
         return mapping.findForward("relogin");
        }
     String flag = request.getParameter("flag");
     String loadpathto = request.getParameter("loadpathto");
   String filePath = request.getRealPath("/"); //取当前系统路径
     String pathphoto = loadpathto;
  
  
   if (form instanceof uploadphotoForm) {//如果form是uploadsForm
     String encoding = request.getCharacterEncoding();
      if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
         {
             response.setContentType("text/html; charset=GBK");//如果没有指定编码,编码格式为GBk
        }
      uploadphotoForm theForm = (uploadphotoForm ) form;
     FormFile file = theForm.getUploadphoto();//取得上传的文件
    try {
       InputStream stream = file.getInputStream(); //把文件读入
 
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
     if (file.getFileSize() > (200 * 1024)) {
       request.setAttribute("message", "文件请控制在200K以下!");
        return mapping.findForward("ErrorInfo");
     }
     String contentType = file.getContentType();
      System.out.println("文件类别:=" + contentType);
     String filebackstr = "";
    if (contentType.equals("image/pjpeg")) {
      filebackstr = ".jpg";
      } else if (contentType.equals("image/gif")) {
       filebackstr = ".gif";
    } else if (contentType.equals("image/bmp")) {
       filebackstr = ".bmp";
     } else {
         request.setAttribute("message", "文件格式不正确!");
          return mapping.findForward("ErrorInfo");
      }
       OutputStream bos = new FileOutputStream(filePath + pathphoto + userid + 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();
        }
        catch(Exception e){
         System.err.print(e);
       }

// 问题 2 所在它的sql语句  where userid="+userid; 是不是要从 数据库种提取出来

        String sql="update sync4j_user set MYFACEPATH='" +pathphoto.replaceFirst("/","") +userid+file.getFileName()+"' where userid="+userid;
       db.doSql(sql, db.DELETE_MODE, conn);
        }else{
        request.setAttribute("message", "上传有误");
           return mapping.findForward("ErrorInfo");
      }
   request.setAttribute("msg", "上传成功");
   return mapping.findForward("loadok");
 

    }
   }

 

  //  第二个类是它的 uploadphotoForm 

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.feng.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.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;
import org.apache.struts.validator.ValidatorForm;

public class uploadphotoForm extends ActionForm {
  protected FormFile uploadphoto;

 public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {
  // TODO Auto-generated method stub
  ActionErrors  errors =  null;
  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;
   }

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

 public FormFile getUploadphoto() {
  return uploadphoto;
 }

 public void setUploadphoto(FormFile uploadphoto) {
  this.uploadphoto = uploadphoto;
 }
}

 

 

      第三个是我的一个上传页面 uploadphoto.jsp页面

<%@page contentType="text/html; charset=GBK" import="java.util.HashMap"%>


<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)


<style>
<!--

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>
<form id="uploadphoto_f" name="uploadphoto_f" enctype="multipart/form-data" method="post" action="../uploadphoto.do">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
 
  <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>

</table>
 </form>
</body>

  </html>
  <%String msg=request.getAttribute("msg")==null?"":request.getAttribute("msg")+"";
  if(!msg.equals("")){
%>
<script>
opener.document.location.href="Userinfo.do";
window.close();
</script>
 
<%}%>
 

请问各位大侠,我这两个类可不可以连接到上传文件哦 我是的是Struts1.2, 是不是需要一个数据传输对像DTO层,从数据库中把它的值提取出来了,谢谢给位大侠帮我这个忙!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

立恒的主页 立恒 | 初学一级 | 园豆:180
提问于:2008-12-19 13:26
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册