cos上传,并生成缩略图

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

<%@ page import="com.oreilly.servlet.multipart.*,com.oreilly.servlet.*"%>

<%@ page import="com.smsgov2.dy.sql.*,java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*"%>

<%

//设置POST请求的内容最大字节为10M,该类用于解析HTTP请求

MultipartParser mp = new MultipartParser(request, 10*1024*1024);

String driver = "com.mysql.jdbc.Driver";

String url = "jdbc:mysql://192.168.1.2:8796/test?useUnicode=true&characterEncoding=gb2312";

String user = "“;

String pass = "";

String type = "";

MyDataBaseManager_2 mdbm = new MyDataBaseManager_2(driver,url,user,pass);

String saveDirectory ="e://upload";

//所有表单域都是Part实例

Part part;

//遍历请求中的所有表单域

while ((part = mp.readNextPart()) != null){

//取得表单域的name属性值

String name = part.getName();

//对于普通表单域

if (part.isParam()){

//取得普通表单域的值

if(name.equals("type")){

ParamPart paramPart = (ParamPart) part;

String value = paramPart.getStringValue("GBK");

out.print("普通表单域部分:<br> name=" + name + "; value=" + value + "<br>");

type = value;

}

}

//对于文件域

else if (part.isFile()){

//取得文件上传域

FilePart filePart = (FilePart) part;

String fileName = filePart.getFileName();

if (fileName != null){



java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMddHHmmss");

    String time = sdf.format(new java.util.Date());



   //输出文件内容

   long size = filePart.writeTo(new File(saveDirectory));

  

    String tempFileName= saveDirectory+"//" + fileName;

    String lastFilePath = saveDirectory+"//"+time+fileName.substring(fileName.lastIndexOf("."));

    String lastFilePath_sl = saveDirectory+"//"+time+"sl"+fileName.substring(fileName.lastIndexOf("."));

    new File(tempFileName).renameTo(new File(lastFilePath));

  

  

    if(lastFilePath.indexOf("")==-1){

       String strtemp ="";

             for(int i=0; i<lastFilePath.length();i++){

     if(lastFilePath.charAt(i)=='//'){

      strtemp+="";

     }else{

        strtemp+=lastFilePath.charAt(i);

     }

    }

       lastFilePath = strtemp;

   }

   if(lastFilePath_sl.indexOf("")==-1){

       String strtemp ="";

             for(int i=0; i<lastFilePath_sl.length();i++){

     if(lastFilePath_sl.charAt(i)=='//'){

      strtemp+="";

     }else{

        strtemp+=lastFilePath_sl.charAt(i);

     }

    }

       lastFilePath_sl = strtemp;

   }

  

   String newurl = lastFilePath_sl; //新的缩略图保存地址

try{

   java.io.File file = new java.io.File(lastFilePath);

  

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



}catch (Exception e){

   e.toString();

}

  

   out.println("上传文件:<br> 文件域的名=" + name + "; 文件名=" + lastFilePath + "<br>" +

   "上传文件的路径=" + filePart.getFilePath()+"<br>"

   +"文件内容类型=" + filePart.getContentType() +

   "<br>"+"文件大小=" + size + "<br>");

  

   mdbm.open();

   int i = mdbm.executeUpdate("insert into test (name,type,slurl) values ('"+lastFilePath+"','"+type+"','"+lastFilePath_sl+"') ");

   mdbm.close();

  

  

}

//文件名为空

else{

   //该文件域没有输入文件名

   out.println("file: name=" + name + "; EMPTY");

}

out.flush();

}

}



mdbm.open();



java.sql.ResultSet rs = mdbm.executeQuery("select * from test");

while(rs!=null && rs.next()){

%>

<img src="<%=rs.getString("name")%>" />   <%=rs.getString("type")%> <br />

<%

}

mdbm.close();



%>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>enctype属性测试</title>

</head>



<body>

<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="upload2.jsp">

请求参数 <input type="text" name="type" /><br>

上传文件: <input type="file" name="file" /><br>

上传文件: <input type="file" name="file2" /><br>

上传文件: <input type="file" name="file3" /><br>

<input name="dd" type="submit" value="提交" />

</form>

</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值