关于上传(upload)的一点啾啾

背景:

在Java 中上传其实比较麻烦,需要引用一些便捷的Java 搞定,比如常见fileupload 等jar,在本站中并没有使用apache upload jar,而采用了

cos-26Dec2008

Maven 地址:


            com.jfinal
            cos
            26Dec2008
        

有兴趣的可以研究下源码,毕竟都是开源的;

下面贴几段代码供大家看看,

1、上传的Action 代码

/**
	 * 上传文件
	 */
	public void upload() {
		UploadFile uploadFile = getFile();
		String newPath = AttachmentUtils.moveFile(uploadFile);
		User user = getLoginedUser();

		processImage(newPath);

		String upload_read_path= OptionQuery.me().findValue("upload_read_path");
		if(upload_read_path!=null){
			String 	path = newPath.substring(newPath.indexOf(dateFormat.format(new Date())));
			newPath= upload_read_path+path;
			newPath=	newPath.replaceAll("\\\\","/");
		}

		Attachment attachment = new Attachment();
		attachment.setUserId(user.getId());
		attachment.setCreated(new Date());
		attachment.setTitle(uploadFile.getOriginalFileName());
		attachment.setPath(newPath.replace("\\", "/"));
		attachment.setSuffix(FileUtils.getSuffix(uploadFile.getFileName()));
		attachment.setMimeType(uploadFile.getContentType());

		attachment.save();
		renderJson("location", JFinal.me().getContextPath() + newPath);
	}

 

2、图片处理

	private void processImage(String newPath) {
		if (!AttachmentUtils.isImage(newPath))
			return;
		
		if(".gif".equalsIgnoreCase(FileUtils.getSuffix(newPath))){
			// 过滤 .gif 图片
			return;
		}

		try {
			// 由于内存不够等原因可能会出未知问题
			processThumbnail(newPath);
		} catch (Throwable e) {
			log.error("processThumbnail error", e);
		}
		try {
			// 由于内存不够等原因可能会出未知问题
			processWatermark(newPath);
		} catch (Throwable e) {
			log.error("processWatermark error", e);
		}
	}

3、贴个Image的关键代码

public class ImageUtils {
	private static final Log log = Log.getLog(ImageUtils.class);

	public static int[] ratio(String src) throws IOException {
		BufferedImage bufferedImage = ImageIO.read(new File(src));
		int width = bufferedImage.getWidth();
		int height = bufferedImage.getHeight();
		return new int[] { width, height };
	}

	public static String ratioAsString(String src) throws IOException {
		File file = new File(src);
		if (!file.exists()) {
			return null;
		}
		BufferedImage bufferedImage = ImageIO.read(file);
		int width = bufferedImage.getWidth();
		int height = bufferedImage.getHeight();
		return String.format("%s x %s", width, height);
	}

	public static String scale(String src, int w, int h) throws IOException {
		int inserTo = src.lastIndexOf(".");
		String dest = src.substring(0, inserTo) + String.format("_%sx%s", w, h) + src.substring(inserTo, src.length());
		scale(src, dest, w, h);
		return dest;
	}

	/**
	 * 等比缩放,居中剪切
	 * 
	 * @param src
	 * @param dest
	 * @param w
	 * @param h
	 * @throws IOException
	 */
	public static void scale(String src, String dest, int w, int h) throws IOException {
		String srcSuffix = src.substring(src.lastIndexOf(".") + 1);
		Iterator iterator = ImageIO.getImageReadersByFormatName(srcSuffix);
		ImageReader reader = (ImageReader) iterator.next();

		InputStream in = new FileInputStream(src);
		ImageInputStream iis = ImageIO.createImageInputStream(in);
		reader.setInput(iis);

		BufferedImage srcBuffered = readBuffereImage(reader, w, h);
		BufferedImage targetBuffered = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

		Graphics graphics = targetBuffered.getGraphics();
		graphics.drawImage(srcBuffered.getScaledInstance(w, h, Image.SCALE_DEFAULT), 0, 0, null); // 绘制缩小后的图

		graphics.dispose();
		srcBuffered.flush();

		ImageIO.write(targetBuffered, srcSuffix, new File(dest));
		targetBuffered.flush();
	}

	private static BufferedImage readBuffereImage(ImageReader reader, int w, int h) throws IOException {
		ImageReadParam param = reader.getDefaultReadParam();
		int srcWidth = reader.getWidth(0);
		int srcHeight = reader.getHeight(0);

		Rectangle rect = null;

		if (w / h > srcWidth / srcHeight) {
			h = h * srcWidth / w;
			w = srcWidth;
			rect = new Rectangle(0, (srcHeight - h) / 2, w, h);
		} else {
			w = w * srcHeight / h;
			h = srcHeight;
			rect = new Rectangle((srcWidth - w) / 2, 0, w, h);
		}
		param.setSourceRegion(rect);
		BufferedImage srcBuffered = reader.read(0, param);
		return srcBuffered;
	}

	public final static void pressImage(String watermarkImg, String srcImageFile) {
		pressImage(watermarkImg, srcImageFile, srcImageFile, 5, -1, -1, 0.2f, 1);
	}

	public final static void pressImage(String watermarkImg, String srcImageFile, String destImageFile) {
		pressImage(watermarkImg, srcImageFile, destImageFile, 5, -1, -1, 0.2f, 1);
	}

	public final static void pressImage(String watermarkImg, String srcImageFile, String destImageFile, int position,
			float alpha) {
		pressImage(watermarkImg, srcImageFile, destImageFile, position, -1, -1, 0.2f, alpha);
	}

	/**
	 * @param watermarkImg
	 *            水印图片位置
	 * @param srcImageFile
	 *            源图片位置
	 * @param destImageFile
	 *            生成的图片位置
	 * @param position
	 *            水印打印的位置: 1->左上角,2->右上角,1->居中,1->左下角,1->右下角
	 * @param xOffset
	 *            x轴偏移量,xOffset小于0,自动偏移
	 * @param yOffset
	 *            y轴偏移量,yOffset小于0,自动偏移
	 * @param radio
	 *            默认为原图的 1/4
	 * @param alpha
	 *            透明度(0~1),PNG图片建议设置为1
	 */
	public final static void pressImage(String watermarkImg, String srcImageFile, String destImageFile, int position,
			int xOffset, int yOffset, float radio, float alpha) {
		try {
			File img = new File(srcImageFile);
			Image src = ImageIO.read(img);
			int srcWidth = src.getWidth(null);
			int srcHeight = src.getHeight(null);

			BufferedImage image = new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_RGB);
			Graphics2D graphics = image.createGraphics();
			graphics.drawImage(src, 0, 0, srcWidth, srcHeight, null);

			// 水印文件
			Image wmImage = ImageIO.read(new File(watermarkImg));
			int wmWidth = wmImage.getWidth(null);
			int wmHeight = wmImage.getHeight(null);

			radio = radio <= 0 ? 0.2f : radio;
			int newWidth = (int) (srcWidth * radio);
			int newHeight = (int) (wmHeight * (newWidth / (float) wmWidth));

			xOffset = (xOffset < 0) ? (int) (newWidth * 0.1f) : xOffset;
			yOffset = (yOffset < 0) ? (int) (newHeight * 0.1f) : yOffset;

			int xPostion = 0;
			int yPostion = 0;

			switch (position) {
			case 1:
				xPostion = xOffset;
				yPostion = yOffset;
				break;
			case 2:
				xPostion = (int) (srcWidth * (1 - radio) - xOffset);
				yPostion = yOffset;
				break;
			case 3:
				xPostion = (int) (srcWidth - newWidth) / 2;
				yPostion = (int) (srcHeight - newHeight) / 2;
				break;
			case 4:
				xPostion = xOffset;
				yPostion = (int) (srcHeight - newHeight - yOffset);
				break;
			case 5:
				xPostion = (int) (srcWidth * (1 - radio) - xOffset);
				yPostion = (int) (srcHeight - newHeight - yOffset);
				break;
			default:
				xPostion = xOffset;
				yPostion = yOffset;
				break;
			}

			graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
			graphics.drawImage(wmImage, xPostion, yPostion, newWidth, newHeight, null);
			// 水印文件结束
			graphics.dispose();
			ImageIO.write((BufferedImage) image, "JPEG", new File(destImageFile));
		} catch (Exception e) {
			log.warn("ImageUtils pressImage error", e);
		}
	}

 

这个里卖弄本来想用thumbnailator-0.4.8.jar解决的,后来...其主要是穷,后面说为什么?

嘿嘿,就分享到这里,具体代码详见DUZHI_BLOG

 

 

 

除特别注明外,本站所有文章均为duzhi原创,转载请注明出处来自https://www.duzhi.me/article/38.html

联系我们

******

在线咨询:点击这里给我发消息

邮件:ashang.peng#aliyun.com

QR code