Resin的图片上传

java 代码
  1. <%@ include file="include/include_clear.jsp"%>   
  2. <%@ page contentType="text/html; charset=GB18030" language="java"%>   
  3. <%@ page import="java.io.*"%>   
  4. <%@ page import="java.util.*"%>   
  5.   
  6.   
  7. <%   
  8.     String DOCROOT = "/home/httpd/pdb";   
  9.     String UPDIR = "/upload";   
  10.     String fileName = request.getParameter("load");   
  11.     String fileExt = request.getParameter("load.filename");//取到文件类型   
  12.     FileInputStream source = null;   
  13.     FileOutputStream destination = null;   
  14.     String ext = "";   
  15.     String exts[] = {"jpg","gif"};   
  16.     int flag = 1;//初始判断为假(不符合文件类型的)   
  17.     String destfile = "";   
  18.     out.println(fileExt);   
  19.        
  20.     if(fileExt.lastIndexOf(46) > 0){   
  21.         int start = fileExt.lastIndexOf(46) + 1//判断"."   
  22.         ext = fileExt.substring(start, fileExt.length()).toLowerCase();//转换大写JSP/GIF   
  23.     }   
  24.        
  25.     for(int i=0;i<exts.length;i++){   
  26.         if(exts[i].equals(ext)){   
  27.             flag = 0;//符合文件类型     
  28.             break;   
  29.         }   
  30.        
  31.     }   
  32.   
  33. if(flag==0){   
  34.   
  35.     int bytes_read = 0;   
  36.        
  37.     try{   
  38.         source = new FileInputStream(fileName);   
  39.         Calendar now = Calendar.getInstance();   
  40.   
  41.         destfile = UPDIR+"/" +now.get(Calendar.YEAR)+(1+now.get(Calendar.MONTH))+ "/"+System.currentTimeMillis()+"."+ext;//取年月时间生成相应目录   
  42.         String dir = DOCROOT+destfile.substring(0,destfile.lastIndexOf("/"));   
  43.            
  44.         File Fdir = new File(dir);   
  45.         if (!Fdir.isDirectory()) {Fdir.mkdir();}//根据取到的年月时间生成相应目录          
  46.            
  47.         out.println(destfile);   
  48.            
  49.         destination = new FileOutputStream(DOCROOT+destfile);   
  50.         byte[] buffer = new byte[1024];   
  51.            
  52.         for(;;){   
  53.                
  54.             bytes_read=source.read(buffer);   
  55.             if(bytes_read==-1break;   
  56.             destination.write(buffer,0,bytes_read);   
  57.         }    
  58.            
  59.     }catch(Exception e){    
  60.         out.println(e);   
  61.         e.printStackTrace();   
  62.     }finally {   
  63.                 if (source!=null) source.close();   
  64.         if (destination!=null) destination.close();   
  65.             
  66.         }   
  67. }else{   
  68. out.println("文件类型 error");   
  69.   
  70. }   
  71.        
  72. %>   
  73.   
  74. <img src="<%=destfile%>">   
  75.   
  76.   
  77.   
  78.   
  79.   
  80.   
  81. 在 resin.conf 的web-app里 加上<multipart-form upload-max='1024kb'/>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值