文件和表单同时上传功能的实现

  最近一起在忙文件上传这一功能模块,文件上传的部份到时蛮简单的    但是要成功的接收表单的参数  却不是很容易的一件事  表单接受参数总是空值    后来接收的是乱码     好不容易总算是成功了 
所以拿出来和大家分享一下    大家以后在做这一块的时候也能少些麻烦  有关可以借鉴的地方   希望自己能叙述清楚  
   高手轻拍   我也是菜鸟级人物………………


下面是word的文档   是我写的一些方法吧  有兴趣的朋友可以下载看下…………

 

addInfo.jsp

uploadimage.jsp

 

<%@ page contentType="text/html;charset=GB18030" language="java" import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,java.sql.*,com.jspsmart.upload.*,java.util.*"%>
<%@ page import="org.apache.commons.dbutils.QueryRunner"%>
<%@ page import="org.apache.commons.dbutils.handlers.BeanListHandler"%>
<%@ page import="cn.wn.demo.DbUtils"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'uploadimage.jsp' starting page</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">
	-->

  </head>
  
  <body>
    <%
     SmartUpload mySmartUpload =new SmartUpload();
     
     //response.setContentType("text/html;charset=utf-8"); 
     long file_size_max=4000000;   // 图片最大尺寸接近3.8MB
     String fileName2="",ext="",testvar="";
     String url="images/";      //应保证在根目录中有此目录的存在      String url="uploadfile/images/"; 
     //初始化
     mySmartUpload.initialize(pageContext);

     //只允许上载此类文件
     try {
        mySmartUpload.setAllowedFilesList("jpg,gif,png,bmp,JPG,GIF,PNG,BMP");//这个地方是限制文件上传的格式,对于文件 要保证图片能正常的上传
        // 上载文件
        mySmartUpload.upload();
     } catch (Exception e){
      %>
      <SCRIPT language=javascript>
      alert("只允许上传.jpg、.gif、.png、.bmp,.BMP,.JPG,.PNG类型图片文件"); 
      
      window.location='addInfo.jsp';
      </script>
      <%
     }
    try{

    com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
    if (myFile.isMissing()){%>
    <SCRIPT language=javascript>
    alert("请先选择要上传的文件");
    window.location='addInfo.jsp';
    </script>
    <%}
    else{
      String myFileName=myFile.getFileName(); //取得上载的文件的文件名
      ext= myFile.getFileExt();      //取得后缀名
      int file_size=myFile.getSize();     //取得文件的大小 
      String saveurl="";
      if(file_size<file_size_max){
        //更改文件名,取得当前上传时间的毫秒数值
        Calendar calendar = Calendar.getInstance();
        String filename = String.valueOf(calendar.getTimeInMillis());
        saveurl=request.getRealPath("/")+url;
        saveurl+=myFileName;          //保存路径
        myFile.saveAs(saveurl,mySmartUpload.SAVE_PHYSICAL);
        System.out.println(myFileName);
        
        String FileName = myFileName;
        session.setAttribute("FileName",FileName);
        
        //-----------------------上传完成,开始生成缩略图 -------------------------   
        java.io.File file = new java.io.File(saveurl);        //读入刚才上传的文件
        String newurl=request.getRealPath("/")+url+filename+"_min."+ext;  //新的缩略图保存地址
        Image src = javax.imageio.ImageIO.read(file);                     //构造Image对象
        float tagsize=200;
        int old_w=src.getWidth(null);                                     //得到源图宽
        int old_h=src.getHeight(null);  
        int new_w=0;
        int new_h=0;                            //得到源图长
        int tempsize;
        float tempdouble;
        if(old_w>old_h){
          tempdouble=old_w/tagsize;
        }else{
          tempdouble=old_h/tagsize;
        }
       new_w=Math.round(old_w/tempdouble);
       new_h=Math.round(old_h/tempdouble);    //计算新图长宽
       BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
       tag.getGraphics().drawImage(src,0,0,new_w,new_h,null);       //绘制缩小后的图
       FileOutputStream newimage=new FileOutputStream(newurl);          //输出到文件流
       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);      
       encoder.encode(tag);                                               //近JPEG编码
       newimage.close();   
       
       //---------------------------这个地方是重点----------------------------
       //----------------表单的数据输入都是以ISOxxxxxxxxxxxx格式的   但是  我们现在要使用中文的字符   这必然就需要转码
       //----你的项目可能是utf-8形式编码的,这个地方没有问题    其中的gbk不能改成utf-8形式   否则就可能会出现乱码的情况
       //----数据中的编码只要也是utf-8的话  就没有什么问题    这个我也进行过测试的
      
      
       
       String goodsName = mySmartUpload.getRequest().getParameter("goodsName");
       goodsName = new String(goodsName.getBytes(), "GBK");
       System.out.println(goodsName);
       

       String goodsIntroduction = mySmartUpload.getRequest().getParameter("goodsIntroduction");
       goodsIntroduction = new String(goodsIntroduction.getBytes(), "GBK");
       System.out.println(goodsIntroduction);

       String goodsFeature = mySmartUpload.getRequest().getParameter("goodsFeature");
       goodsFeature = new String(goodsFeature.getBytes(), "GBK");
        System.out.println(goodsFeature);


       String goodsSize = mySmartUpload.getRequest().getParameter("goodsSize");
       goodsSize = new String(goodsSize.getBytes(), "GBK");
       System.out.println(goodsSize);
       
       System.out.println("-----------------------这里-----------------------------------");

       String sql = null;
		try {

			QueryRunner qr = DbUtils.getQueryRunner();
			// 为sql赋值,插入语句
			sql = "insert into goods (goodsName,goodsIntroduction,goodsFeature,goodsSize) values(?,?,?,?)";
			String params[] = { goodsName,  FileName, goodsIntroduction, goodsFeature, goodsSize };

			qr.update(sql, params);
			
			//session.invalidate();
		} catch (Exception e) {
			e.printStackTrace();
		}
       
       out.print("<SCRIPT language='javascript'>");
       out.print("alert('上传文件大成功');");
       out.print("window.location='/project/admin/uploadimage.jsp';");
       out.print("</SCRIPT>");
     }
   }

    
   }catch (Exception e){

   e.toString();     //e.printStackTrace();

  }
  %>
  </body>
</html>

 




上面数据操作部分  是自己写的组件  可能大家下载用不起来   因为这个是说明文件上传的   所以部在说了 


要是不明白的大虾   可以邮件给我   459675517@qq.com   大家交流  交流




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值