⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileutil.java

📁 软件工程资料
💻 JAVA
字号:
package com.cnpoint.myspaces.common.util;



import java.io.File;
import java.io.InputStream;

import org.apache.commons.fileupload.*;
import org.apache.commons.logging.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;

public class FileUtil {
	private static Log log = LogFactory.getLog(FileUtil.class);
//	当上传文件超过限制时设定的临时文件位置
	private String tempPath = "C:\\TEMP";

//	文件上传目标目录
	private String destinationPath;

//	获取的上传请求
	private HttpServletRequest fileuploadRequest = null;

//	设置最多只允许在内存中存储的数据,单位:字节
	//private int sizeThreshold = 4096;
	private int sizeThreshold = 4096000;
//	设置允许用户上传文件大小,单位:字节
//	共10M
	private long sizeMax = 10*10485760;
	//private long sizeMax = 40960000 ;
	//private long sizeMax = -1;
	
	String fileName = null;
 
	public FileUtil(){
		
	}

	public FileUtil(String tempPath, String destinationPath){
		this.tempPath = tempPath;
		this.destinationPath = destinationPath;
	}

	public FileUtil(String tempPath, String destinationPath, HttpServletRequest fileuploadRequest){
		this.tempPath = tempPath;
		this.destinationPath = destinationPath;
		this.fileuploadRequest = fileuploadRequest;
	}

	/** 文件上载
	* @return true ?? success; false ?? fail.
	*/
	public boolean Upload(){

//	如果没有临时目录,则创建它
	if(!(new File(tempPath).isDirectory())){
	try{
	new File(tempPath).mkdirs();
	}catch(SecurityException e){
	  //System.out.println("can not make security direcoty");
		log.info(e);
	}
	}

//	如果没有上传目的目录,则创建它
	if(!(new File(destinationPath).isDirectory())){
	try{
	new File(destinationPath).mkdirs();
	}catch(SecurityException e){
	System.out.println("can not make security direcoty");
	}
	}

//	上传项目只要足够小,就应该保留在内存里。 
//	较大的项目应该被写在硬盘的临时文件上。 
//	非常大的上传请求应该避免。 
//	限制项目在内存中所占的空间,限制最大的上传请求,并且设定临时文件的位置。

	DiskFileUpload fu = new DiskFileUpload();

//	设置最多只允许在内存中存储的数据,单位:字节
	fu.setSizeThreshold(sizeThreshold);

//	设置允许用户上传文件大小,单位:字节
//	10M
	fu.setSizeMax(sizeMax);

//	设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录 
	fu.setRepositoryPath(tempPath); 

	Iterator iter = null;

//	读取上传信息 
	try {
		List fileItems = fu.parseRequest(fileuploadRequest);
//		处理上传项目
//		依次处理每个上传的文件 
		iter = fileItems.iterator(); 

	} catch (FileUploadException e) {
	   log.info("上传文件过大");
	   //e.printStackTrace();
	   log.info(e.getMessage());
	   return false;
	} 

	while (iter.hasNext()) { 
		FileItem item = (FileItem) iter.next(); 

//		忽略其他不是文件域的所有表单信息 
		if (!item.isFormField()) {

//			上传的是文件信息
			//String fieldName = item.getFieldName();
			String name = item.getName();

			if((name == null) || name.equals("") && item.getSize() == 0){
			continue;
		}

	    fileName = this.GetFileName(name);
	    setFileName(System.currentTimeMillis()+"-"+fileName);
		try {
			//FileOutputStream fos = new FileOutputStream(new File(this.destinationPath + fileName));
			FileOutputStream fos = new FileOutputStream(new File(this.destinationPath + getFileName() ));
			InputStream uploadStream = item.getInputStream();
			BufferedInputStream bis = new BufferedInputStream(uploadStream);
			byte b[] = new byte[409600];
			int nRead;
			while( (nRead = bis.read(b,0,409600)) >0){
				fos.write(b, 0, nRead);
			}
			uploadStream.close();
			bis.close();
			fos.close();
		 
		} catch (Exception e) {
		  //e.printStackTrace();
		  log.info(e.getMessage());
		  return false;
		}
	   
		}else{
//			上传的是普通表单字域
			String fieldName = item.getFieldName();
			String name = item.getName();

			 
			//System.out.println(fieldName);			
			if(fieldName.equals("docID")){
				setDocID(item.getString());
			}

			if(fieldName.equals("docName")){
				setDocName(item.getString());
			}
			
			if(fieldName.equals("curedition")){
				setCuredition(item.getString());
			}
			
			if((name == null) || name.equals("") && item.getSize() == 0){
			  continue;
			}
		}
	
	}

	return true;
	 
	
	}

	/**从路径中获取单独文件名
	* @author Huangye
	*
	* TODO 要更改此生成的类型注释的模板,请转至
	* 窗口 - 首选项 - Java - 代码样式 - 代码模板
	*/
	public String GetFileName(String filepath)
	{
	String returnstr = "*.*";
	int length = filepath.trim().length();

	filepath = filepath.replace('\\', '/');
	if(length >0)
	{
	int i = filepath.lastIndexOf("/");
	if (i >= 0)
	{
	filepath = filepath.substring(i + 1);
	returnstr = filepath;
	}
	}
	return returnstr;
	}
	/**
	* 省略所有的getXXX和setXXX访问器方法
	*/
	public  String getFileName(){
		
		return fileName;
	}
	
	public void setFileName(String fileName){
		this.fileName = fileName;
	}
	
	String docName;
	String docID;
	String curedition;

	public String getCuredition() {
		return curedition;
	}

	public void setCuredition(String curedition) {
		this.curedition = curedition;
	}

	public String getDocID() {
		return docID;
	}

	public void setDocID(String docID) {
		this.docID = docID;
	}

	public String getDocName() {
		return docName;
	}

	public void setDocName(String docName) {
		this.docName = docName;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -