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

📄 uploadfile.java

📁 java实现的上传文件的代码
💻 JAVA
字号:
/*
 *  Copyright 2007-2008 The ccgj.
 *
 *  Licensed under the ccgj software License, Version 1.0;
 *  you may not use this file except in compliance with the License.
 *  
 *  You can obtain a copy of the soft license in publish project or
 *  from ccgj web site.
 *
 */
package com.ccgj.platform.mp3;

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.Properties;

import org.apache.struts.upload.FormFile;

import com.ccgj.common.conf.ConfigureFile;
import com.ccgj.database.beans.ClpMstMp3T;

/**
 * 파일         업로드          기능 
 * 
 * 
 * @author 
 * 
 */
public class UploadFile {
	private static Properties pro = null;

	private static String mp3FilePath = "";
	private static String mp3FileTryPath = "";

	/**
	 * 
	 * @param file
	 * @param ad
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static boolean buildDivContextFile(FormFile file, ClpMstMp3T t)
					throws FileNotFoundException, IOException {
		// 업로드 경로 획득
		mp3FilePath = ConfigureFile.getMessage("mp3_Download");
		mp3FileTryPath = ConfigureFile.getMessage("mp3_DownloadTry");
		String str = "";
		String strTry = "";
		String acceStr = "";
		//파일  업로드 상세 경로 획득
		synchronized (UploadFile.class) {
			File dirFile = new File(mp3FilePath);
			if (!dirFile.exists() || !dirFile.isDirectory()) {
				dirFile.mkdirs();
			} else {
				File[] fileList = dirFile.listFiles();
				if (fileList.length <= 0) {
					str = mp3FilePath + (fileList.length + 1) + "/";
					acceStr = String.valueOf(fileList.length + 1);
				} else {
					str = mp3FilePath + fileList.length + "/";
					acceStr = String.valueOf(fileList.length);
				}
				File dirFile1 = new File(str);
				File[] fileList1 = dirFile1.listFiles();
				if (!dirFile1.exists() || !dirFile1.isDirectory()) {
					dirFile1.mkdirs();
				} else {
					if (fileList1.length < 1000) {
						str = mp3FilePath + fileList.length + "/";
						acceStr = String.valueOf(fileList.length);
					} else {
						str = mp3FilePath + (fileList.length + 1) + "/";
						acceStr = String.valueOf(fileList.length + 1);
						File dirFile2 = new File(str);
						if (!dirFile2.exists() || !dirFile2.isDirectory()) {
							dirFile2.mkdirs();
						}
					}

				}
			}
			dirFile = new File(mp3FileTryPath);
			if (!dirFile.exists() || !dirFile.isDirectory()) {
				dirFile.mkdirs();
			} else {
				File[] fileList = dirFile.listFiles();
				if (fileList.length <= 0) {
					strTry = mp3FileTryPath + (fileList.length + 1) + "/";
					acceStr = String.valueOf(fileList.length + 1);
				} else {
					strTry = mp3FileTryPath + fileList.length + "/";
					acceStr = String.valueOf(fileList.length);
				}
				File dirFile1 = new File(strTry);
				File[] fileList1 = dirFile1.listFiles();
				if (!dirFile1.exists() || !dirFile1.isDirectory()) {
					dirFile1.mkdirs();
				} else {
					if (fileList1.length < 1000) {
						strTry = mp3FileTryPath + fileList.length + "/";
						acceStr = String.valueOf(fileList.length);
					} else {
						strTry = mp3FileTryPath + (fileList.length + 1) + "/";
						acceStr = String.valueOf(fileList.length + 1);
						File dirFile2 = new File(strTry);
						if (!dirFile2.exists() || !dirFile2.isDirectory()) {
							dirFile2.mkdirs();
						}
					}

				}
			}
			// 파일 업로드 
			int theIndex = 0;
			String fname = new String(file.getFileName().getBytes(), "utf-8");
			theIndex = fname.indexOf(".");
			String suffix = fname.substring(theIndex);
			System.out.println(suffix);
			String fileName = "mp3Record" + t.getNo() + suffix;
			System.out.println(fileName);
			InputStream streamIn = file.getInputStream();

			OutputStream streamOut = new FileOutputStream(str + fileName);

			int bytesRead = 0;
			byte[] buffer = new byte[Integer.parseInt(com.ccgj.common.conf.ConfigureFile.getMessage("upload_buffer"))];
			while ((bytesRead = streamIn.read(buffer, 0, buffer.length)) != -1) {
				streamOut.write(buffer, 0, bytesRead);
			}
			// 파일 커트 기능
			streamIn = file.getInputStream();
			OutputStream streamOut1 = new FileOutputStream(strTry + fileName);
			int i = 0;
			while ((bytesRead = streamIn.read(buffer, 0, buffer.length)) != -1) {
				if (i > 200)
					break;
				streamOut1.write(buffer, 0, bytesRead);
				i++;
			}
			streamOut.close();
			streamIn.close();
			file.destroy();
			t.setAccesories(acceStr + "/" + fileName);

		}
		return true;
	}
	
}

⌨️ 快捷键说明

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