文件上传类,通用

package com.beijian.action;

import com.beijian.common.util.FileUtil;
import com.beijian.common.util.ImageMarkUtil;
import com.beijian.common.util.ImageZipUtil;
import com.beijian.common.util.PropertiesUtil;
import com.beijian.function.SysconfigFuc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;

@Controller
public class UpFileAction extends BaseAction {
	private static final String cfg_imgtype = "cfg_imgtype";
	private static final String cfg_attachtype = "cfg_attachtype";
	private static final String cfg_mediatype = "cfg_mediatype";
	private static final String cfg_ddimg_width = "cfg_ddimg_width";
	private static final String cfg_ctrl_img = "cfg_ctrl_img";
	private static final String cfg_imagemark = "cfg_imagemark";
	private static final String cfg_imagemarktype = "cfg_imagemarktype";
	private static final String cfg_imagemarkdegree = "cfg_imagemarkdegree";
	private static final String cfg_imagemarkalpha = "cfg_imagemarkalpha";
	private static final String cfg_imagemarkwidth = "cfg_imagemarkwidth";
	private static final String cfg_imagemarkheight = "cfg_imagemarkheight";
	private static final String cfg_imagemaricon = "cfg_imagemaricon";
	private static final String cfg_imagemarktext = "cfg_imagemarktext";
	private static final String cfg_imageforntsize = "cfg_imageforntsize";

	private static final String cfg_imagemarkbold = "cfg_imagemarkbold";

	private static final String cfg_smallimage = "cfg_smallimage";
	private static final String cfg_middleimage = "cfg_middleimage";
	private static final String cfg_bigimage = "cfg_bigimage";
	private static final String _$26 = "text/html; charset=UTF-8";
	private String proUtil = PropertiesUtil.getRootpath();
	public int IMG_WIDTH = 100;
	public File uploadifyfile;
	private final String uploads = "uploads";
	private static final String images = "images";
	public String IMAGE_TYPE = "";
	private static final int three = 3;
	private static final String video = "video";
	public String FLASH_TYPE = "";
	private static final int fifty = 50;
	private static final String file = "file";
	public String FILE_TYPE = "";
	private static final int ten = 10;
	private String temp_file_path = "";
	public String temp_file_type = "";
	public int temp_file_size = 0;
	public String uploadifyfileFileName;
	public String isImgControl;
	public String isYin;
	public String isImgControl_isYin;

	
	public String executeUpFlash() throws Exception {
		this.FLASH_TYPE = SysconfigFuc.getSysValue(cfg_mediatype);
		if (this.temp_file_path.equals("")) {
			setTemp_file_path(video);
			setTemp_file_size(fifty);
			setTemp_file_type(this.FLASH_TYPE);
		}
		String imageSavePath = getImageSavePath();
		String uploadFileName = new FileUtil().getFileExt(this.uploadifyfileFileName);
		String fileNameByDate = createFileNameByDate(uploadFileName);
		String file_type = "";
		
		if (this.temp_file_type.indexOf(uploadFileName) == -1) {
			file_type = this.temp_file_type + " is allowed";
		} else {
			InputStream in = new FileInputStream(this.uploadifyfile);
			OutputStream out = new FileOutputStream(new File(imageSavePath
					+ File.separator + fileNameByDate));
			try {
				byte[] arrayOfByte = new byte[1048576];
				while (in.read(arrayOfByte) > 0)
					out.write(arrayOfByte);
			} catch (Exception strException) {
				strException.printStackTrace();
				file_type = "upload fail";
			} finally {
				if (in != null)
					in.close();
				if (out != null)
					out.close();
			}
		}
		String ss = "/uploads/" + this.temp_file_path + "/"
				+ getYearMonthDay() + "/" + fileNameByDate;
		if (!file_type.equals("")) {
			ss = "";
		}
		
		
		HttpServletResponse response = getResponse();
		PrintWriter printWriter = response
				.getWriter();
		response.getWriter().print(ss);
		return "none";
	}

	public String executeUpFile() throws Exception {
		this.FILE_TYPE = SysconfigFuc.getSysValue(cfg_imgtype).toString();
		if (this.temp_file_path.equals("")) {
			setTemp_file_path(file);
			setTemp_file_size(ten);
			setTemp_file_type(this.FILE_TYPE);
		}
		String imageSavePath = getImageSavePath();
		String uploadFileName = new FileUtil().getFileExt(this.uploadifyfileFileName);
		String fileNameByDate = createFileNameByDate(uploadFileName);
		String file_type = "";
		if (this.temp_file_type.indexOf(uploadFileName) == -1) {
			file_type = this.temp_file_type + " is allowed";
		} else {
			InputStream input = new FileInputStream(this.uploadifyfile);
			OutputStream out = new FileOutputStream(new File(imageSavePath
					+ File.separator + fileNameByDate));
			try {
				byte[] arrayOfByte = new byte[1048576];
				while (input.read(arrayOfByte) > 0)
					out.write(arrayOfByte);
			} catch (Exception strException) {
				strException.printStackTrace();
				file_type = "upload fail";
			} finally {
				if (input != null)
					input.close();
				if (out != null)
					out.close();
			}
		}
		String path = "/uploads/" + this.temp_file_path + "/"
				+ getYearMonthDay() + "/" + fileNameByDate;
		if (!file_type.equals("")) {
			path = "";
		}
		HttpServletResponse response = getResponse();
		PrintWriter printWriter = response.getWriter();
		response.getWriter().print(path);
		return "none";
	}

	public String executeUpGoodsimages() throws Exception {
		this.IMG_WIDTH = Integer.parseInt(SysconfigFuc
				.getSysValue(cfg_ddimg_width));
		this.IMAGE_TYPE = SysconfigFuc.getSysValue(cfg_imgtype).toString();
		if (this.temp_file_path.equals("")) {
			setTemp_file_path(images);
			setTemp_file_size(three);
			setTemp_file_type(this.IMAGE_TYPE);
		}
		String imageSavePath = getImageSavePath();
		String uploadFileName = new FileUtil().getFileExt(this.uploadifyfileFileName);
		String fileNameByDate = createFileNameByDate(uploadFileName);
		String file_type = fileNameByDate.replace("." + uploadFileName, "");
		String str5 = "";
		
		if (this.temp_file_type.indexOf(uploadFileName) == -1) {
			str5 = this.temp_file_type + " is allowed";
		} else {
			InputStream input = new FileInputStream(this.uploadifyfile);
			OutputStream out = new FileOutputStream(new File(imageSavePath
					+ File.separator + fileNameByDate));
			try {
				byte[] arrayOfByte = new byte[1048576];
				while (input.read(arrayOfByte) > 0) {
					out.write(arrayOfByte);
					out.flush();
				}
			} catch (Exception strException) {
				strException.printStackTrace();
			} finally {
				if (input != null)
					input.close();
				if (out != null)
					out.close();
			}
		}
		
		String path = "uploads/" + this.temp_file_path + "/" + getYearMonthDay();
		
		String pUtil = this.proUtil + path + "/" + fileNameByDate;
		String str6 = "";
		int i = pUtil.lastIndexOf(".");
		if (i > -1) {
			str6 = pUtil.substring(0, i);
		}
		String str7 = str6 + "_small." + uploadFileName;
		FileUtil fileUtil = new FileUtil();
		fileUtil.copyFile(pUtil, str7);
		String str8 = str6 + '.' + uploadFileName;
		String str9 = str6 + "_big." + uploadFileName;
		fileUtil.copyFile(pUtil, str9);
		String str10 = "/" + path + "/" + file_type + "." + uploadFileName;
		
		HttpServletResponse response = getResponse();
		response.setCharacterEncoding("UTF-8");
		PrintWriter printWriter = response.getWriter();
		printWriter.write(str10);
		ImageZipUtil strImageZipUtil = new ImageZipUtil();
		String str11 = SysconfigFuc.getSysValue(cfg_smallimage).toString();
		int k;
		
		if (str11.indexOf(",") > -1) {
			String[] array = str11.split(",");
			if (array.length > 1) {
				int j = Integer.parseInt(array[0].toString());
				k = Integer.parseInt(array[1].toString());
				strImageZipUtil.imageZipProce(str7, j, k, 1.0F);
			}
		}
		String value1 = SysconfigFuc.getSysValue(cfg_middleimage)
				.toString();
		int m = 0;
		if (value1.indexOf(",") > -1) {
			String[] array1 = value1.split(",");
			if (array1.length > 1) {
				k = Integer.parseInt(array1[0].toString());
				m = Integer.parseInt(array1[1].toString());
				strImageZipUtil.imageZipProce(str8, k, m, 1.0F);
			}
		}
		String value = SysconfigFuc.getSysValue(cfg_middleimage)
				.toString();
		if (value.indexOf(",") > -1) {
			String[] array = value.split(",");
			if (array.length > 1) {
				m = Integer.parseInt(array[0].toString());
				int n = Integer.parseInt(array[1].toString());
				strImageZipUtil.imageZipProce(str9, m, n, 1.0F);
			}
		}
		return "none";
	}

	public String executeUpimages() throws Exception {
		this.IMG_WIDTH = Integer.parseInt(SysconfigFuc
				.getSysValue(cfg_ddimg_width));
		this.IMAGE_TYPE = SysconfigFuc.getSysValue(cfg_imgtype).toString();
		if (this.temp_file_path.equals("")) {
			setTemp_file_path(images);
			setTemp_file_size(three);
			setTemp_file_type(this.IMAGE_TYPE);
		}
		String imageSavePath = getImageSavePath();
		String uploadFileName = new FileUtil().getFileExt(this.uploadifyfileFileName);
		String fileNameByDate = createFileNameByDate(uploadFileName);
		String file_type = "";
		if (this.temp_file_type.indexOf(uploadFileName) == -1) {
			file_type = this.temp_file_type + " is allowed";
		} else {
			InputStream input = new FileInputStream(this.uploadifyfile);
			OutputStream out = new FileOutputStream(new File(imageSavePath
					+ File.separator + fileNameByDate));
			try {
				byte[] arrayOfByte = new byte[1048576];
				while (input.read(arrayOfByte) > 0)
					out.write(arrayOfByte);
			} catch (Exception strException) {
				strException.printStackTrace();
				file_type = "upload fail";
			} finally {
				if (input != null)
					input.close();
				if (out != null)
					out.close();
			}
		}
		String path = "/uploads/" + this.temp_file_path + "/"
				+ getYearMonthDay() + "/" + fileNameByDate;
		
		
		if (!file_type.equals("")) {
			path = "";
		}

		HttpServletResponse response = getResponse();
		PrintWriter printWriter =response
				.getWriter();
		response.getWriter().print(path);
		
		String str5 = "";
		
		if ((this.isImgControl != null) && (!"".equals(this.isImgControl))) {
			str5 = SysconfigFuc.getSysValue(cfg_ctrl_img);
			if ((str5 != null) && (!str5.equals("")))
				zipimagesproces(imageSavePath + File.separator + fileNameByDate, str5);
		} else if ((this.isYin != null) && (!"".equals(this.isYin))) {
			str5 = SysconfigFuc.getSysValue(cfg_ddimg_width);
			if ((str5 != null) && (!str5.equals("")))
				zipimagesproces(imageSavePath + File.separator + fileNameByDate, str5);
			imageMarkProcess(imageSavePath + File.separator + fileNameByDate);
		} else if ((this.isImgControl_isYin != null)
				&& (!"".equals(this.isImgControl_isYin))) {
			str5 = SysconfigFuc.getSysValue(cfg_ctrl_img);
			if ((str5 != null) && (!str5.equals("")))
				zipimagesproces(imageSavePath + File.separator + fileNameByDate, str5);
			imageMarkProcess(imageSavePath + File.separator + fileNameByDate);
		} else {
			str5 = SysconfigFuc.getSysValue(cfg_ddimg_width);
			if ((str5 != null) && (!str5.equals("")))
				zipimagesproces(imageSavePath + File.separator + fileNameByDate, str5);
		}
		return "none";
	}

	public void zipimagesproces(String paramString1, String paramString2) {
		ImageZipUtil imageZip = new ImageZipUtil();
		Integer strInteger = Integer.valueOf(0);
		if ((paramString2 != null) && (!paramString2.equals(""))
				&& (!paramString2.equals("0"))) {
			strInteger = Integer.valueOf(Integer.parseInt(paramString2));
			imageZip.imageZipProce(paramString1, strInteger
					.intValue(), strInteger.intValue(), 1.0F);
		}
	}

	public void imageMarkProcess(String paramString) {
		ImageMarkUtil imageMarkUtil = new ImageMarkUtil();
		String str1 = "";
		String str2 = "";
		Integer integer1 = Integer.valueOf(0);
		Integer integer2 = Integer.valueOf(0);
		Integer integer3 = Integer.valueOf(0);
		Integer integer4 = Integer.valueOf(0);
		
		if (!SysconfigFuc.getSysValue(cfg_imagemark).equals("1")) {
			str1 = SysconfigFuc.getSysValue(cfg_imagemaricon);
			integer1 = Integer.valueOf(Integer.parseInt(SysconfigFuc
					.getSysValue(cfg_imagemarkdegree)));
			integer2 = Integer.valueOf(Integer.parseInt(SysconfigFuc
					.getSysValue(cfg_imagemarkwidth)));
			integer3 = Integer.valueOf(Integer.parseInt(SysconfigFuc
					.getSysValue(cfg_imagemarkheight)));
			Float strFloat = Float.valueOf(Float.parseFloat(SysconfigFuc
					.getSysValue(cfg_imagemarkalpha)));
			integer4 = Integer.valueOf(Integer.parseInt(SysconfigFuc
					.getSysValue(cfg_imageforntsize)));
			str2 = SysconfigFuc.getSysValue(cfg_imagemarktext);
			if (SysconfigFuc.getSysValue(cfg_imagemarktype).equals("0"))
				imageMarkUtil.markImageByIcon(str1, paramString,
						paramString, integer1, strFloat.floatValue(),
						integer2, integer3);
			else if (SysconfigFuc.getSysValue(cfg_imagemarktype).equals("1"))
				imageMarkUtil.markByText(str2, paramString, paramString,
						integer1, strFloat.floatValue(), integer2,
						integer3, integer4);
		}
	}

	public String execute() throws ServletException, IOException {
		if (this.temp_file_path.equals("")) {
			setTemp_file_path(images);
			setTemp_file_size(three);
			setTemp_file_type(this.IMAGE_TYPE);
		}
		HttpServletRequest request = getRequest();
		HttpServletResponse response = getResponse();
		String imageSavePath = getImageSavePath();
		System.out.print("路径为:======:" + imageSavePath);
		File file1 = new File(imageSavePath);
		if (!file1.exists())
			file1.mkdirs();
		DiskFileItemFactory disfDiskFileItemFactory = new DiskFileItemFactory();
		ServletFileUpload servletFileUpload = new ServletFileUpload(
				disfDiskFileItemFactory);
		servletFileUpload.setHeaderEncoding("UTF-8");
		List list = null;
		try {
			list = servletFileUpload.parseRequest(request);
			System.out.print("文件个数为:======:" + list.size());
		} catch (FileUploadException fileUploadException) {
			return null;
		}
		Iterator it = list.iterator();
		String str2 = "";
		String str3 = "";
		while (it.hasNext()) {
			FileItem fileItem = (FileItem) it.next();
			if (!fileItem.isFormField()) {
				str2 = fileItem.getName();
				long l = fileItem.getSize();
				String str4 = fileItem.getContentType();
				System.out.println(l + "字节" + " " + str4);
				if ((str2 != null) && (!str2.trim().equals(""))) {
					if (str2.lastIndexOf(".") >= 0)
						str3 = str2.substring(str2.lastIndexOf("."));
					File file2 = null;
					do {
						Date date = new Date();
						SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
								"HHmmssSSS");
						str2 = simpleDateFormat.format(date);
						file2 = new File(imageSavePath + str2 + str3);
					} while (file2.exists());
					File ff = new File(imageSavePath + str2 + str3);
					try {
						fileItem.write(ff);
					} catch (Exception strException) {
						strException.printStackTrace();
					}
				}
			}
		}
		response.getWriter().print(str2 + str3);
		return "none";
	}

	public String createFileNameByDate(String paramString) {
		Date strDate = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"HHmmssSSS");
		String str = simpleDateFormat.format(strDate) + "."
				+ paramString;
		return str;
	}

	public String getImageSavePath() {
		String imageSavePath = PropertiesUtil.getRootpath() + uploads + File.separator
				+ this.temp_file_path;
		String str2 = getYearMonthDay();
		imageSavePath = imageSavePath + File.separator + str2;
		File file = new File(imageSavePath);
		if (!file.exists())
			file.mkdirs();
		return imageSavePath;
	}

	public String getYearMonthDay() {
		Date strDate = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyyMMdd");
		return simpleDateFormat.format(strDate);
	}

	public File getUploadifyfile() {
		return this.uploadifyfile;
	}

	public void setUploadifyfile(File paramFile) {
		this.uploadifyfile = paramFile;
	}

	public String getUPLOADS_PATH() {
		return uploads;
	}

	public String getIMAGES_PATH() {
		return images;
	}

	public String getTemp_file_path() {
		return this.temp_file_path;
	}

	public void setTemp_file_path(String paramString) {
		this.temp_file_path = paramString;
	}

	public String getTemp_file_type() {
		return this.temp_file_type;
	}

	public void setTemp_file_type(String paramString) {
		this.temp_file_type = paramString;
	}

	public int getTemp_file_size() {
		return this.temp_file_size;
	}

	public void setTemp_file_size(int paramInt) {
		this.temp_file_size = paramInt;
	}

	public String getIMAGE_TYPE() {
		return this.IMAGE_TYPE;
	}

	public int getIMAGE_SIZE() {
		return three;
	}

	public String getUploadifyfileFileName() {
		return this.uploadifyfileFileName;
	}

	public void setUploadifyfileFileName(String paramString) {
		this.uploadifyfileFileName = paramString;
	}

	public String getIsImgControl() {
		return this.isImgControl;
	}

	public void setIsImgControl(String paramString) {
		this.isImgControl = paramString;
	}

	public String getIsYin() {
		return this.isYin;
	}

	public void setIsYin(String paramString) {
		this.isYin = paramString;
	}

	public String getIsImgControl_isYin() {
		return this.isImgControl_isYin;
	}

	public void setIsImgControl_isYin(String paramString) {
		this.isImgControl_isYin = paramString;
	}
}

你可能感兴趣的:(文件上传与下载)