前段需要写个上传文件的功能

前段需要写个上传文件的功能,现在就把他贴上来。

 

package  com.xiaobus.util;

import  java.io.File;
import  java.io.FileOutputStream;
import  java.io.InputStream;
import  java.io.OutputStream;
import  java.text.SimpleDateFormat;
import  java.util.Date;
import  java.util.Random;

import  javax.servlet.http.HttpServletRequest;
import  org.apache.struts.upload.FormFile;



/**
 * <pre>
 * ------------------------------
 * Name: xiaobus System
 * Author: Shawn
 * CopyRight: cama company
 * WebSite: 
http://blog.csdn.net/xdy3008
 * Email: xiao.dongyi@gmail.com
 * Date: Sep 3, 2007
 * ------------------------------
 * </pre>
 
*/


public   class  FileUpLoadUtil  {
    
    
public final static String imageFlag_Old_New = "new";

    
// 上传文件,返回文件名字
    public static String uploadFile(FormFile file, HttpServletRequest request) {
        String uploadFileAllowedExt 
= PropertiesCache.getInstance()
                .getUploadFileAllowedExt();
        
long uploadFileMaxSize = PropertiesCache.getInstance()
                .getUploadFileMaxSize();
        String uploadFileDirectory 
= PropertiesCache.getInstance()
                .getUploadFileDirectory();

        String fileName 
= null;
        String filePath 
= null;
        
int len = file.getFileName().length();
        
int tempIndex = file.getFileName().lastIndexOf(".");
        String ext 
= file.getFileName().substring(tempIndex + 1, len);

        
try {
            
if (file.getFileSize() < uploadFileMaxSize
                    
&& uploadFileAllowedExt.indexOf(ext) != -1{
                InputStream stream 
= file.getInputStream();
                String contextPath 
= request.getRealPath("/");
                contextPath 
= contextPath + uploadFileDirectory;
                fileName 
= createFileName(ext);
                
if (fileName == null{
                    
return null;
                }

                filePath 
= createFilePath(uploadFileDirectory,contextPath);
                OutputStream bos 
= new FileOutputStream(filePath + fileName);
                
int bytesRead = 0;
                
byte[] buffer = new byte[8192];
                
while ((bytesRead = stream.read(buffer, 08192)) != -1{
                    bos.write(buffer, 
0, bytesRead);
                }

                bos.close();
                stream.close();
                
int index = filePath.indexOf(uploadFileDirectory);
                
int length = filePath.length();
                
return filePath.substring(index + uploadFileDirectory.length(),
                        length)
                        
+ fileName;
            }
 else {
                
return null;
            }

        }
 catch (Exception e) {
            e.printStackTrace();
            System.out.println(
"Error happan when uploadFile.");
            
return null;

        }

    }


    
public static String createFilePath(String uploadFileDirectory,String contextPath) {
        Date today 
= new Date();
        String year, month, day;
        year 
= new SimpleDateFormat("yyyy").format(today);
        month 
= new SimpleDateFormat("MM").format(today);
        day 
= new SimpleDateFormat("dd").format(today);
        
        
//如果没有上传文件的文件夹,就创建一个
        if (!(new File(contextPath).exists())) {
            
new File(contextPath).mkdir();
        }

        
        
        String dic 
= contextPath + "/" + year;
        
if (!(new File(dic).exists())) {
            
new File(dic).mkdir();
        }

        dic 
= dic + "/" + month;
        
if (!(new File(dic).exists())) {
            
new File(dic).mkdir();
        }

        dic 
= dic + "/" + day;
        
if (!(new File(dic).exists())) {
            
new File(dic).mkdir();
        }

        
return dic + "/";
    }


    
public static String createFileName(String ext) {
        
if (ext.equals("")) {
            
return null;
        }

        Random r 
= new Random();
        
return "" + System.currentTimeMillis() + r.nextInt(9999+ "." + ext;
    }


    
public static String createFileNameNew(String ext) {
        
if (ext.equals("")) {
            
return null;
        }

        
return "file" + System.currentTimeMillis() + imageFlag_Old_New + "." + ext;
    }

    

    
    
public static void main(String[] args) {
        String a 
= "c:/";
        String dic 
= a + "AAAAAAAAAAAA";
        
if (!(new File(dic).exists())) {
            
new File(dic).mkdir();
            System.out.println(
"no , but now create one.");
        }

        
else{
            System.out.println(
"exist !");
        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值