Java上传方法及相关代码

// 上传
public   boolean  SaveFile( byte [] binData, String fileName) {
        
boolean  success  =   false ;
        File file 
=   new  File(fileName);
        file.getParentFile().mkdirs();
        
        
try  {
            BufferedOutputStream bufferedOutputStream 
=   new  BufferedOutputStream(
                    
new  FileOutputStream(file));
            bufferedOutputStream.write(binData);
            bufferedOutputStream.close();
            success 
=   true ;
        } 
catch  (IOException e) {
            e.printStackTrace();
        }

        
return  success;
    }

// 测试方法
public   static   void  main(String[] args) {
        FileUpload fu 
=   new  FileUpload();

        File fileS 
=   new  File( " f://nod32激活码.txt " );

        
if  ( ! fileS.exists()) {
            System.out.println(
" 找不到指定文件 " );
            
return ;
        }
        FileInputStream fileIS 
=   null ;
        
byte [] binData  =   null ;
        
try  {
            fileIS 
=   new  FileInputStream(fileS);
            binData 
=   new   byte [( int ) fileS.length()];
            BufferedInputStream bufferedInputStream 
=   new  BufferedInputStream(
                    
new  FileInputStream(fileS));
            
int  r  =  bufferedInputStream.read(binData);
            
if  (r  !=  fileS.length())
                
throw   new  IOException( " 读取文件不正确 " );
            bufferedInputStream.close();
        } 
catch  (FileNotFoundException ex) {
            ex.printStackTrace();
        } 
catch  (IOException ex) {
            ex.printStackTrace();
        } 
finally  {
            
if  (fileIS  !=   null ) {
                
try  {
                    fileIS.close();
                } 
catch  (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }

        
boolean  f  =  fu.SaveFile(binData,  " c://11.txt " );
    }

你可能感兴趣的:(Java上传方法及相关代码)