我遇到的问题JspSmartUpload组件实现表单和文件同时上传

  这个问题,困扰我几天。之前一直以为应该很好解决,但直到真正解决起来才知道他的难度,而解决后又觉得其实不是很难。看来自己当初多么的无知。

   现在说明一下结局方法。

   要提交的jsp页面addmodel.jsp:

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="com.mobile.service.convert" %> 
<%
 request.setCharacterEncoding("gb2312");
%>
<html>
<jsp:useBean id="db" scope="page" class="com.mobile.conn.mysql"/>
<head>
<title>add model</title>
<link rel="stylesheet" type="text/css" href="../css/style1.css"/>
<script type="text/javascript" src="../js/calendar.js"></script>
<script language="javascript">

 var CalendarWebControl = new atCalendarControl();
 
 function changepic(){
  var fileName=document.all.file.value;
  if(fileName=="")
  document.all.pic.src="../img/product/nopic.jpg";
  var exName=fileName.substring(fileName.lastIndexOf(".")+1).toUpperCase()
  if(exName=="JPG"||exName=="GIF")
   document.all.pic.src=document.all.file.value;
  else{
   alert("清选择图片格式为gif,jpg");   
   document.all.pic.src="../img/product/nopic.jpg";
  }
 }
</script>
</head>
<body>
<form name="form1" action="../domodel" method="POST" ENCTYPE="multipart/form-data">
  <input type="hidden" name="kind" value="1">
<div align="center">
  <center>
  <table border="0" width="519" cellspacing="0" cellpadding="0" height="243">
    <tr>
      <td width="517" height="38" align="center" bgcolor="#FFFFFF" valign="top"><font size="5" color="#FFCC66">添加手机</font>
</td>
    </tr>
    <tr>
      <td width="517" height="253">
        <table border="0" width="100%" cellspacing="0" cellpadding="0" height="174">
          <tr>
            <td width="34%" height="41" align="right">*手机型号:</td>
            <td width="136%" height="41" align="left" valign="middle"><input type="text" name="modelname" size="20" class="txt">
          </tr>
          <tr>
            <td width="34%" height="44" align="right">*所属品牌:</td>
<%
 String sql="select * from label order by idno desc";
 ResultSet rs=db.executeQuery(sql);
%>
            <td width="136%" height="44" align="left"><select size="1" name="labidno">
<%while(rs.next()){%>             
     <option value="<%=rs.getInt("idno")%>"><%=convert.getInstance().iso2gb(rs.getString("cname")) %></option>
<%} %>             
              </select></td>
          </tr>
          <tr>
            <td width="34%" height="43" align="right">手机类型:</td>
            <td width="136%" height="43" align="left"><select size="1" name="modelkind">
                <option selected value="直板">直板</option>
                <option value="翻盖">翻盖</option>
                <option value="滑盖">滑盖</option>
                <option value="其它">其它</option>
              </select></td>
          </tr>
          <tr>
            <td width="34%" height="43" align="right">手机图片:</td>
            <td width="136%" height="43" align="left"><input name="file" type="file" onChange="changepic()" class="txt"><br>
   <img src="../img/product/nopic.jpg" name="pic" width="100" height="200"></td>
          </tr>
          <tr>
            <td width="34%" height="43" align="right">*发布时间:</td>
            <td width="136%" height="43" align="left"><input type="text" name="pubtime" size="20" class="txt" readonly="true">
                  <img src="../img/btn_select_date_1.gif" width="18" height="18" onClick="CalendarWebControl.show(document.form1.pubtime,'',this)"></td>
          </tr>
        </table>
      </td>
    </tr>
    <TR>
     <td align="center">
     <input type="submit" value="提交" class="btt"> <input type="reset" value="重填" class="btt">
     </td>
    </tr>
  </table>
  </center>

</div> 
</form>
</body>
</html>

   有点长看看我用颜色标记的地方就行。

下面是重点看看接收数据的servlet domodel.java

package com.mobile.service;

import javax.servlet.http.*;
import javax.servlet.jsp.*;

import java.io.*;

import javax.servlet.*;

import java.sql.*;
import com.mobile.conn.*;

import java.util.*;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.File;

public class domodel extends HttpServlet {
 private mysql mysql=new mysql();
 public String modelname;
 public String modelimg;
 public String modelkind;
 private ServletConfig config;
 
 final public void init(ServletConfig config) throws ServletException {
    this.config = config;
 }

 
 public void doGet(HttpServletRequest request,HttpServletResponse response)
  throws IOException,ServletException{
  response.setContentType("text/html;charset=gb2312");
  request.setCharacterEncoding("gb2312");
  response.setCharacterEncoding("gb2312");
  PrintWriter out=response.getWriter();
  SmartUpload smtup=new SmartUpload();
  smtup.initialize(config,request,response); //初始化
  try{
   smtup.upload();
         int kind=Integer.parseInt((smtup.getRequest()).getParameter("kind"));//这个就是接收了表单的kind数据
         switch(kind){
         case 1:
          
         }
  }
  catch(Exception e){
   out.print(e.toString());
  }
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doGet(request,response);
 } 
}

终结一下要点:1.表单的页面要将表单的属性即<form ENCTYPE="multipart/form-data">如上设置。表示表单上传的是多媒体数据

            2.servlet的初始化实现config类。用做实现jspsmartupload类初始化的参数。用pagecontext也行。

            3.实现JspSmartUpload类后要马上执行upload方法表示将表单所有数据接收过来

            4.在第3步执行后使用(smtup.getRequest()).getParameter("kind")接受直接使用request对象不好用只能先使用getRequest()方法在进行接收

最后,感谢网上提供参考资料的作者们!http://www.webjx.com/htmldata/2005-08-19/1124406463.html非常感谢以上连接文章的作者,给我很大帮助!

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wp500

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值