关闭

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

标签: importfilenullbufferwebsiteext
522人阅读 评论(0) 收藏 举报
分类:

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

 

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:180138次
    • 积分:2852
    • 等级:
    • 排名:第12999名
    • 原创:78篇
    • 转载:113篇
    • 译文:3篇
    • 评论:25条
    最新评论