« 用ext做的一个登录页面« »EXT中键盘触发事件的处理 »
Ext中的文件上传笔记,呵呵!

话还得从头说起,因为这一段时间以来我们公司正在用ext来做后台的管理系统,而我做的那一块里面包含了一个照片上传的功能 ,
因为对ext还不是太熟,所以以为它其中的文件上传控件还要自己去重写它的控件,可没想到上一下它的论坛一搜,原来只要在控件
里面将其xtype设置为file,再将form里面的fileUpload设置为true就行了,这时以为问题解决了,就在action里面用String来接收上传
控件里面的内容,可是搞了大半天还是收不到啥东西,后来经师兄点醒才知道原来直接用file对象接收就行了(晕,当然,这不是ext
的问题,是我自己菜的原因)。
用file对象接收后我采用了一个同事的建议将其转化成字节流后存入一个字节数组然后再存入数据库,(因为对java中的图像操作不熟,所以也搞了一点时间,)
不过后来还是搞定了:
这是转化后保存的代码:

 

Java代码 复制代码
  1. public void save() throws Exception   
  2.     {   
  3.         logger.debug("id:" + student.getId());             
  4.         logger.debug("photo:" + (photoObj==null));   
  5.        
  6.         HashMap<String, Object> result = this.getDefaultMap();   
  7.         List<Student> list = new ArrayList<Student>();   
  8.         try  
  9.         {   
  10.                
  11.             if(photoObj!=null)   
  12.             {   
  13.             logger.debug("here:");   
  14.                
  15.             BufferedImage img=ImageIO.read(photoObj);   
  16.                
  17.                int h = img.getHeight();   
  18.                int w = img.getWidth();   
  19.                    
  20.                 byte[] data = new byte[h*w];   
  21.                 ByteArrayOutputStream out = new ByteArrayOutputStream();   
  22.                 BufferedImage dest = new BufferedImage(w, h,   
  23.                         BufferedImage.TYPE_3BYTE_BGR);   
  24.                 dest.getGraphics().drawImage(img, 00, w, h, Color.WHITE,   
  25.                         null);   
  26.                 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);   
  27.                 encoder.encode(dest);   
  28.                 data = out.toByteArray();   
  29.                    
  30.                 logger.debug("data.length:" + data.length);   
  31.                 student.setPhoto(data);    
  32.                    
  33.                 String outname="c:\\\\img3.jpg";           
  34.                 FileOutputStream fileoutputstream = new FileOutputStream(outname);   
  35.                  fileoutputstream.write(data);    
  36.                  fileoutputstream.close();    
  37.                     
  38.                 out.close();   
  39.                
  40.                    
  41.             studentManager.save(student);   
  42.             list.add(student);   
  43.             result.put(COUNT, 1);   
  44.             result.put(LIST, 1);   
  45.             }   
  46.         }    
  47.         catch (VersionException se)   
  48.         {   
  49.             result.put(SUCCESS, false);   
  50.             result.put(MESSAGE, "该数据已被其他用户修改,请重新加载编辑窗口.");   
  51.         }   
  52.         catch (Exception se)   
  53.         {   
  54.             result.put(SUCCESS, false);   
  55.             result.put(MESSAGE, "Error unknown.");   
  56.         }   
  57.         this.getResponse().flushBuffer();   
  58.         outJson(result);   
  59.   
  60.     }   
  61. 这里是读出到网页的代码:   
  62.   
  63. public void image()   
  64.             throws Exception {   
  65.            
  66.         String id=data.get("myid");    
  67.         System.out.println("id:"+id);   
  68.         if(id!=null)   
  69.         {   
  70.         Long lid=Long.valueOf(id);   
  71.                
  72.         //设置浏览器不缓存图片   
  73.         this.getResponse().setHeader("Pragma","No-cache");   
  74.         this.getResponse().setHeader("Cache-Control","no-cache");   
  75.         this.getResponse().setDateHeader("Expires"0);   
  76.         this.getResponse().addHeader("Content-disposition""inline" + "; filename=\"" + "safs.jpg" + "\"");   
  77.   
  78.         this.getResponse().setContentType("image/jpeg");   
  79.            
  80.            
  81.         OutputStream os=this.getResponse().getOutputStream();   
  82.         byte[] mybyte=studentManager.getObject(lid).getPhoto();   
  83.            
  84.            
  85.         os.write(mybyte);   
  86.         os.close();   
  87.         }   
  88.         else  
  89.         {   
  90.             logger.debug("This record there isn't photo!");   
  91.         }   
  92.   
  93.     }  


搞定后听另外一个同事说可以直接用一种叫Blob的类型来接收后直接写入数据库就行了,刚好那里要下班了所以就
没去试,不过后来回宿舍后自己试了一下,还真的可以,比先转化成字节流再写入来得直接点。不过因为我们用的pgsql数据库,不支持
Blob,所以我还是决定用bytea来存byte[]算了,以后有机会再用Blob来存吧,呵呵:
以下是我用mysql试验一下的代码:
 

Java代码 复制代码
  1. import java.io.File;   
  2. import java.io.FileInputStream;   
  3. import java.io.FileOutputStream;   
  4. import java.io.InputStream;   
  5. import java.sql.Connection;   
  6. import java.sql.DriverManager;   
  7. import java.sql.PreparedStatement;   
  8. import java.sql.ResultSet;   
  9.   
  10.   
  11. public class Dbtest {   
  12.     private static final String URL = "jdbc:mysql://localhost:3306/jiejie?user=root&password=&useUnicode=true";   
  13.     private Connection conn = null;   
  14.     private PreparedStatement pstmt = null;   
  15.     private ResultSet rs = null;   
  16.     private File file = null;   
  17.       
  18.        
  19.     /**  
  20.     * 向数据库中插入一个新的BLOB对象(图片)  
  21.     * @param infile 要输入的数据文件  
  22.     * @throws java.lang.Exception  
  23.     */  
  24.    public void blobInsert(String infile) throws Exception   
  25.    {   
  26.        FileInputStream fis = null;     
  27.            try  
  28.            {   
  29.                Class.forName("org.gjt.mm.mysql.Driver").newInstance();   
  30.                conn = DriverManager.getConnection(URL);   
  31.               
  32.                file = new File(infile);   
  33.                fis = new FileInputStream(file);   
  34.                pstmt = conn.prepareStatement("insert into tmp(descs,pic) values(?,?)");   
  35.                pstmt.setString(1,file.getName());    //把传过来的第一个参数设为文件名   
  36.                pstmt.setBinaryStream(2,fis,fis.available());  //第二个参数为文件的内容   
  37.                pstmt.executeUpdate();   
  38.       }   
  39.            catch(Exception ex)   
  40.            {   
  41.           System.out.println("[blobInsert error : ]" + ex.toString());   
  42.            }   
  43.         finally  
  44.         {   
  45.                //关闭所打开的对像//   
  46.                pstmt.close();   
  47.                fis.close();   
  48.                conn.close();   
  49.            }   
  50.     }   
  51.       
  52.   
  53.     /**  
  54.     * 从数据库中读出BLOB对象  
  55.     * @param outfile 输出的数据文件  
  56.     * @param picID 要取的图片在数据库中的ID  
  57.     * @throws java.lang.Exception  
  58.     */  
  59.   
  60.     public void blobRead(String outfile,String picName) throws Exception   
  61.     {   
  62.         FileOutputStream fos = null;   
  63.         InputStream is = null;   
  64.         byte[] Buffer = new byte[4096];   
  65.     
  66.             try  
  67.             {   
  68.                 Class.forName("org.gjt.mm.mysql.Driver").newInstance();   
  69.                 conn = DriverManager.getConnection(URL);   
  70.                 pstmt = conn.prepareStatement("select pic from tmp where descs=?");   
  71.                 pstmt.setString(1,picName);         //传入要取的图片的ID   
  72.                 rs = pstmt.executeQuery();   
  73.                 rs.next();   
  74.                         
  75.                 file = new File(outfile);   
  76.                 if(!file.exists())   
  77.                 {   
  78.                     file.createNewFile();     //如果文件不存在,则创建   
  79.                 }   
  80.                 fos = new FileOutputStream(file);   
  81.                 is = rs.getBinaryStream("pic");   
  82.                 int size = 0;   
  83.                 while((size = is.read(Buffer)) != -1)   
  84.                 {   
  85.                     //System.out.println(size);   
  86.                     fos.write(Buffer,0,size);   
  87.                 }   
  88.                                  
  89.          }   
  90.             catch(Exception e)   
  91.             {   
  92.                 System.out.println("[OutPutFile error : ]" + e.getMessage());   
  93.             }   
  94.             finally  
  95.             {   
  96.                 //关闭用到的资源   
  97.                 fos.close();   
  98.                 rs.close();   
  99.                 pstmt.close();   
  100.                 conn.close();   
  101.             }   
  102.     }   
  103.        
  104.     public static void main(String[] args)   
  105.     {   
  106.         try  
  107.         {   
  108.               
  109.             Dbtest blob = new Dbtest();   
  110.             //blob.blobInsert("C:/jie1.jpg");             
  111.             blob.blobRead("c:/1.jpg","jie.jpg");   
  112.         }   
  113.         catch(Exception e)   
  114.         {   
  115.             System.out.println("[Main func error: ]" + e.getMessage());   
  116.         }   
  117.     }   
  118.   
  119. }  
 


Tags: Ext  ExtJS  文件上传  java  

原创文章如转载,请注明:转载自:飞扬部落编程仓库 : http://www.busfly.net/csdn/

本文链接地址:http://www.busfly.net/csdn/post/655.html

如果你喜欢本文,请顶一下,支持我,你的支持是我继续发好文章的最大动力。谢谢。
好东西需要分享,快把本文发给你的朋友吧~!~

     
相关文章:
用ext做的一个登录页面  (2008-6-23 11:13:2)
EXT提交服务器的三种方式(转)  (2008-6-23 11:9:9)
Ext中的xtype和vtype  (2008-6-23 11:7:47)
ext 常用类简单说明  (2008-6-23 10:49:54)
围绕Ext JS 2.0的IDE、插件和工具 by Jack Slocum  (2008-6-23 10:44:19)
extJs formpanel小例  (2008-6-23 1:11:35)
Ext-实现带查询以及分页的列表  (2008-6-23 1:9:56)
学习Ext-js之使用XML输出数据  (2008-6-23 1:8:4)
Ext-基本布局的实现  (2008-6-23 1:5:56)
Ext.Template类  (2008-6-23 1:3:26)




◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
Feed订阅集
网站分类
勤劳致富^.^
最近发表
最新评论及回复
最近留言
随机推荐文章
Powered By Z-Blog   STYLE by busfly . FatMouse
Copyright © 2007 巴士飞扬技术博客. . 沪ICP备07027972号. 会员群1(VS为主):3769186.