本地图片上传到文件服务器上并绘制图片

本文介绍了在项目中如何将本地图片上传至文件服务器,并详细讲解了相关实现过程,通过实例代码进行演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在做项目的过程中,有可能会碰到将本地的图片上传到专门的存放静态图片的服务器上,接下来给大家好好介绍下,废话少说,代码上:

/**
	 * 本地图片上传到文件服务器上
	 * @param filePath
	 * @param serverPath
	 * @return
	 * @throws Exception
	 */
	public String LoadImageToServer(String filePath,String serverPath) throws Exception { 

	    String resultPath = "";          //上传后图片所在的路径 
	    FileOutputStream out = null;     //文件输出流 
	    try {                               //验证图片上传的格式是否正确 
	     File f = new File(filePath); 
	        if (!f.isFile()) { 
	        throw new Exception(f+" 不是图片文件!"); 
	    } 
	     if (f != null && f.exists()) {       //这里的ImageIO属于java工厂类,在工厂类class里面,调用的System.gc(),频繁调用会造成dump,需要考虑优化 
	        BufferedImage image = ImageIO.read(f); // 读入文件 
	        if (image != null) { 
	        BufferedImage tag = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);  //构造一个类型为预定义图像类型之一的 BufferedImage 
	           tag.getGraphics().drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);                     //绘制所需要尺寸大小的图片 
	        /* 
	         * 以下生成图片上传后在服务器上的新路径 
	         */ 
	        int lastLength = filePath.lastIndexOf("."); 
	        Date date = new Date(System.currentTimeMillis()); 
	        String strDate = new SimpleDateFormat("yyyyMMddhhmmss").format(date); 
	        int random = (int)(Math.random()*99); 
	        String imageName = strDate+random;                          //以系统时间来随机的创建图片文件名 
	        String fileType = filePath.substring(lastLength);              //获取上传图片的类型 
	        resultPath = serverPath+ "site"+ imageName+ fileType; 
	        /* 
	         * 进行图片的绘制 
	         */ 
	        out = new FileOutputStream(resultPath); 
	        saveAsJPEG(100, tag, 0.95f, out);
	        tag = null; 
	      } 
	     } 
	     f = null; 
	    } catch (Exception ex) { 
	     ex.printStackTrace(); 
	    } finally { 
	     out.close(); 
	     out = null; 
	    } 
	    return null; 
	}
接下来就是上面saveAsJPEG方法的代码:

/**
	 * 以JPEG编码保存图片
	 * 
	 * @param dpi
	 *            分辨率
	 * @param image_to_save
	 *            要处理的图像图片
	 * @param JPEGcompression
	 *            压缩比
	 * @param fos
	 *            文件输出流
	 * @throws IOException
	 */
	public static void saveAsJPEG(Integer dpi, BufferedImage image_to_save,
			float JPEGcompression, FileOutputStream fos) throws IOException {

		ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("jpg").next();
		ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
		imageWriter.setOutput(ios);
		// and metadata
		IIOMetadata imageMetaData = imageWriter.getDefaultImageMetadata(
				new ImageTypeSpecifier(image_to_save), null);

		if (dpi != null && !dpi.equals("")) {
			Element tree = (Element) imageMetaData.getAsTree("javax_imageio_jpeg_image_1.0");
			Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0);
			jfif.setAttribute("Xdensity", Integer.toString(dpi));
			jfif.setAttribute("Ydensity", Integer.toString(dpi));
		}

		if (JPEGcompression >= 0 && JPEGcompression <= 1f) {
			JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter
					.getDefaultWriteParam();
			jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
			jpegParams.setCompressionQuality(JPEGcompression);

		}
		imageWriter.write(imageMetaData,
				new IIOImage(image_to_save, null, null), null);
		ios.close();
		imageWriter.dispose();
	}
其中的Element要导入的包是import org.w3c.dom.Element;其他就没有啥注意事项了,可以用起来啦!嘻嘻!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值