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

📄 upload.java

📁 用于J2EE的上传
💻 JAVA
字号:
package dianxun.util;

import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;

import org.apache.struts.upload.FormFile;

public class Upload {
	private FormFile formFile;
	private String path;
	private String pathName;
	private String sPathName;
	private String realPath;
	private String name;
	private int width;
	private int height;

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public void setFormFile(FormFile formFile) {
		this.formFile = formFile;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getPathName() {
		return pathName;
	}

	public String getSPathName() {
		return sPathName;
	}

	public String randomName() {
		String date = new SimpleDateFormat("yyMMddHHmmss").format(new Date());
		String type = fileType();
		int num = (int) Math.random() * 100;
		return path + date + num + "." + type;
	}

	public boolean isRightFile(int size) {
		if (formFile.toString().equals(""))
			return false;

		int fileSize = formFile.getFileSize();
		size = size * 1024;
		String fileType = this.fileType();
		System.out.println("--------:::" + fileType);
		if (fileSize < size
				&& null != formFile
				&& (fileType.equals("jpg") || fileType.equals("gif") || fileType
						.equals("pjpeg"))) {
			return true;
		} else
			return false;
	}

	public String fileType() {
		String type = formFile.getContentType();
		System.out.println("type:::::" + type);
		String[] foot = type.split("/");
		if (foot[1].equals("pjpeg"))
			return "jpg";
		return foot[1];
	}

	// 删除操作
	public void delete(String path) {
		String realPath = System.getProperty("user.dir")
				+ "\\autodeploy\\dianxun\\" + path;

		System.out.println(realPath);
		File file = new File(realPath);
		if (file.isFile()) {
			System.out.println("找到了");
			file.delete();
		}
	}

	// 获得图片长宽
	public void getHW(String path) {
		String realPath = System.getProperty("user.dir")
				+ "\\autodeploy\\dianxun\\" + path;

		File file = new File(realPath);
		try {
			Image src = javax.imageio.ImageIO.read(file);// 构造Image对象
			this.width = src.getWidth(null);
			this.height = src.getHeight(null);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void upload() {
		System.out.println("到了哪里了?");
		if (formFile != null) {

			try {
				byte[] content = formFile.getFileData();
				realPath = System.getProperty("user.dir")
						+ "\\autodeploy\\dianxun\\upload\\" + path + "\\";
				System.out.println(realPath);
				File file = new File(realPath);
				if (!file.isDirectory())
					file.mkdirs();// 创建文件夹

				name = randomName();
				while (new File(realPath + name).isFile())
					name = randomName();

				System.out.println(realPath + name);
				FileOutputStream out = new FileOutputStream(realPath + name);
				System.out.println("文件开始上传...." + formFile.getFileSize());
				out.write(content);
				System.out.println("文件上传成功....");
				pathName = "upload/" + path + "/" + name;
				out.close();
			} catch (Exception e) {
				System.out.println(e);
			}
		}
	}

	public void smallPic(int nw) {
		try {
			File FileObject = new File(realPath + name); // 取原图
			File smallFile = new File(realPath + "s" + name); // 定义要保存的小图

			AffineTransform transform = new AffineTransform();
			BufferedImage bis = ImageIO.read(FileObject);
			int w = bis.getWidth();
			int h = bis.getHeight();

			int nh = (nw * h) / w;
			double sx = (double) nw / w;
			double sy = (double) nh / h;
			transform.setToScale(sx, sy);
			AffineTransformOp ato = new AffineTransformOp(transform, null);
			BufferedImage bid = new BufferedImage(nw, nh,
					BufferedImage.TYPE_3BYTE_BGR);
			ato.filter(bis, bid);
			ImageIO.write(bid, "jpg", smallFile);
			sPathName = "upload/" + path + "/s" + name;
		} catch (IOException e) {

			System.out.println("缩略图生成有问题!!!");
		}
	}

	public void smallPic(int nw, int nh) {
		try {
			File FileObject = new File(realPath + name); // 取原图
			File smallFile = new File(realPath + "s" + name); // 定义要保存的小图

			AffineTransform transform = new AffineTransform();
			BufferedImage bis = ImageIO.read(FileObject);
			int w = bis.getWidth();
			int h = bis.getHeight();

			double sx = (double) nw / w;
			double sy = (double) nh / h;
			transform.setToScale(sx, sy);
			AffineTransformOp ato = new AffineTransformOp(transform, null);
			BufferedImage bid = new BufferedImage(nw, nh,
					BufferedImage.TYPE_3BYTE_BGR);
			ato.filter(bis, bid);
			ImageIO.write(bid, "jpg", smallFile);
			sPathName = "upload/" + path + "/s" + name;
		} catch (IOException e) {

			System.out.println("缩略图生成有问题!!!");
		}
	}

	public static void main(String[] args) {
		String s = "" + (int) Math.random() * 100;
		System.out.print(s);
	}
}

⌨️ 快捷键说明

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