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

📄 uploadfileservice.java

📁 本代码是点歌系统的部分代码实现了部分功能
💻 JAVA
字号:
package music.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.StringTokenizer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.upload.FormFile;

public class UploadFileService {
	static Log log = LogFactory.getLog(UploadFileService.class);

	String baseUploadPath;

	/**
	 * 获得文件路径
	 * 
	 * @param module_name
	 *            模块名 如“blog”,拼出的路径为……/blog/……
	 * @param unit_name
	 *            单位名,如“1”为1号专家,拼出的路径为……/blog/1/
	 * @param fileName
	 *            文件名
	 * @return
	 */
	public File getFilePath(String module_name, String fileName) {
		String serverpath = "F:/Tomcat 5.5/webapps/onlinemusic";
		StringBuffer sb = new StringBuffer(serverpath).append("/").append(
				module_name).append("/");
		File f = new File(sb.toString());
		if (!f.exists()) {
			f.mkdirs();
		}
		String filename = "" + System.currentTimeMillis();
		String sufix = null;
		StringTokenizer st = new StringTokenizer(fileName, ".");
		int count = st.countTokens();
		if (count <= 1)
			sufix = null;

		while (st.hasMoreTokens()) {
			sufix = st.nextToken();
		}

		File vitualFilePath = new File(sb.toString() + filename + "." + sufix);
		int i = 1;
		while (vitualFilePath.exists()) {
			vitualFilePath = new File(sb.toString() + filename + i + "."
					+ sufix);
			i++;
		}
		// String fname = null;
		// try {
		// fname = new String(sb.toString().getBytes("utf-8"), "utf-8");
		// } catch (UnsupportedEncodingException e) {
		// }
		// f = new File(fname);
		// return f;
		return vitualFilePath;
	}

	/**
	 * 获得url
	 * 
	 * @param file
	 * @return
	 */
	public String getFileUrl(File file) {
		String absolutePath = file.getAbsolutePath();
		String serverpath = "F:/Tomcat 5.5/webapps/onlinemusic";
		return absolutePath.substring(serverpath.length());
	}

	/**
	 * 获得绝对路径
	 * 
	 * @param file
	 * @return
	 */
	public String getFileAbsolutePath(String path) {
		String serverpath = "F:/Tomcat 5.5/webapps/onlinemusic";
		return serverpath + path;

	}

	/**
	 * 保存formfile至文件file
	 * 
	 * @param file
	 * @param f
	 * @param limitSize
	 *            单位:K
	 * @throws UploadFileIsTooBigException
	 */
	public void saveFile(FormFile file, File f,int limitSize) {

		if (!f.exists()) {
			try {
				f.createNewFile();
			} catch (IOException e) {
				log.error(e);
			}
		}
		try {
			InputStream stream = file.getInputStream();
			 if (file.getFileSize() < (limitSize * 1024)) {
			// write the file to the file specified
			OutputStream bos = new FileOutputStream(f.getPath());
			int bytesRead = 0;
			byte[] buffer = new byte[8192];
			while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
				bos.write(buffer, 0, bytesRead);
			}
			bos.close();
			stream.close();

			} else {
			stream.close();
			file.destroy();
			f.delete();

			 }
		} catch (FileNotFoundException fnfe) {
			log.error(fnfe);
		} catch (IOException ioe) {
			log.error(ioe);
		}
		file.destroy();
	}

	// /**
	// * 删除文件
	// *
	// * @param filePath
	// * @return
	// */
	// public boolean deleteFile(String filePath) {
	// File file = new File(filePath);
	// if (file.exists()) {
	// file.delete();
	// return true;
	// }
	// return true;
	// }
	//
	//	
	// public String getContentType(String fileName) {
	// String suffix = null;
	// if (fileName == null) {
	// fileName = "";
	// }
	// StringTokenizer st = new StringTokenizer(fileName, ".");
	// int count = st.countTokens();
	// if (count <= 1) {
	// return Constants.ContentType.UNKNOW;
	// }
	// while (st.hasMoreTokens()) {
	// suffix = st.nextToken();
	// }
	// String[] types = Constants.ContentType.types;
	// String[] names = Constants.ContentType.typeNames;
	// for (int i = 0; i < types.length; i++) {
	// if (suffix.toLowerCase().matches(types[i])) {
	// return names[i];
	// }
	// }
	// return Constants.ContentType.UNKNOW;
	// }
	//
	// /**
	// * @return Returns the baseUploadPath.
	// */
	// public String getBaseUploadPath() {
	// return baseUploadPath;
	// }
	//
	// /**
	// * @param baseUploadPath
	// * The baseUploadPath to set.
	// */
	// public void setBaseUploadPath(String baseUploadPath) {
	// this.baseUploadPath = baseUploadPath;
	// }
}

⌨️ 快捷键说明

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