细心!用心!耐心!

吾非文人,乃市井一俗人也,读百卷书,跨江河千里,故申城一游; 一两滴辛酸,三四年学业,五六点粗墨,七八笔买卖,九十道人情。

BlogJava 联系 聚合 管理
  1 Posts :: 196 Stories :: 10 Comments :: 0 Trackbacks
 1 package  com.stt.doss.common.util.upload;
 2
 3 import  java.io.BufferedInputStream;
 4 import  java.io.BufferedOutputStream;
 5 import  java.io.File;
 6 import  java.io.FileNotFoundException;
 7 import  java.io.FileOutputStream;
 8 import  java.io.IOException;
 9 import  java.io.InputStream;
10 import  java.io.OutputStream;
11
12 import  org.apache.commons.logging.Log;
13 import  org.apache.commons.logging.LogFactory;
14 import  org.apache.struts.upload.FormFile;
15
16 /**
17  * <p>
18  * Title:
19  * </p>
20  * <p>
21  * Description:
22  * </p>
23  * <p>
24  * Copyright: Copyright (c) 2006
25  * </p>
26  * <p>
27  * Company: stt
28  * </p>
29  *
30  *  @author  william
31  *  @version  1.0
32   */

33 public   class  UploadHelper  {
34
35      private   static  Log log  =  LogFactory.getLog(UploadHelper. class );
36
37      public   static   void  uploadFileForm(FormFile formFile, String folderPath,
38             String fileName)  {
39         log.info( " UploadHelper() uploadFileForm start. " );
40          //  retrieve the file data
41         InputStream inputStream  =   null ;
42         OutputStream outputStream  =   null ;
43
44          int  bytesRead  =   0 ;
45          byte [] buffer  =   new   byte [ 8192 ];
46
47         File file  =   new  File(folderPath);
48          if  ( ! file.exists())  {
49              boolean  f  =  file.mkdir();
50              if  ( ! f)  {
51                 f  =  file.mkdirs();
52                  if  ( ! f)  {
53
54                 }

55             }

56
57         }

58
59          try   {
60             StringBuffer filePath  =   new  StringBuffer();
61             filePath.append(folderPath);
62             filePath.append(File.separator);
63             filePath.append(fileName);
64
65             inputStream  =   new  BufferedInputStream(formFile.getInputStream(),
66                      2048 );
67             outputStream  =   new  BufferedOutputStream( new  FileOutputStream(
68                     filePath.toString()),  2048 );
69
70              //  read and write
71              while  ((bytesRead  =  inputStream.read(buffer,  0 8192 ))  !=   - 1 {
72                 outputStream.write(buffer,  0 , bytesRead);
73             }

74         }
  catch  (FileNotFoundException fnfe)  {
75             fnfe.printStackTrace();
76         }
  catch  (IOException ioe)  {
77             ioe.printStackTrace();
78         }
  finally   {
79              try   {
80                  //  close the stream
81                 outputStream.close();
82                 inputStream.close();
83             }
  catch  (IOException ioe)  {
84                 ioe.printStackTrace();
85             }

86              //  destroy the temporary file
87             formFile.destroy();
88         }

89
90     }
  //  end uploadFileForm
91
92      public   static   void  deleteFile(String path)  {
93         log.info( " UploadHelper() deleteFile start. " );
94         File file  =   new  File(path);
95         file.delete();
96     }

97 }

98
posted on 2007-03-16 15:27 张金鹏 阅读(221) 评论(0)  编辑  收藏 所属分类: core java中的一些数据结构的处理

只有注册用户登录后才能发表评论。


网站导航: