上传图片

class  
{
	/**
	* 创建路径
	*/
	private void createDirctory(String path){
  		File f= new File(path);
  		if(!f.exists()){
  			f.mkdirs();
  		}
  	}

	/**
	* 删除路径
	*/
    private void delFileDirectory(String path){
    	File f = new File(path);//定义文件路径         
    	if(f.exists() && f.isDirectory()){//判断是文件还是目录  
    		if(f.listFiles().length==0){//若目录下没有文件则直接删除  
                  f.delete();  
    		}else{//若有则把文件放进数组,并判断是否有下级目录  
    			File delFile[]=f.listFiles();  
    			int i =f.listFiles().length;  
    			for(int j=0;j<i;j++){  
    				if(delFile[j].isDirectory()){  
    					delFileDirectory(delFile[j].getAbsolutePath());//递归调用del方法并取得子目录路径  
    				}
    				delFile[j].delete();
    			}
    		}      
    	}
    }

	/**
	* 删除文件
	*/
	 private void deleteFile(String path) throws Exception{
    	try{
    		File file = new File(path);
    		if(file.exists()){
    			file.delete();
    		}
    	}catch(Exception e){
    		logger.error("经纪人头像图片失败", e);
    	}
    }
}


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.sql.Blob;
import java.sql.SQLException;
class  
{
	/**
	* 将Blob文件以JPG格式写入到本地
	*/
	public void toJpg(Blob blob, String filePath) throws IOException, SQLException {
			BufferedImage bufferedImage = ImageIO.read(blob.getBinaryStream());
			File file = new File(filePath);

			// create a blank, RGB, same width and height, and a white background
			BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
					bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
			newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);

			// write to jpeg file
			ImageIO.write(newBufferedImage, "jpg", file);
	}
	/**
	* 以流的方式将文件以JPG格式写入到本地
	*/
   public void toJpg(InputStream inputStream, String dest) throws IOException {

        BufferedImage bufferedImage = ImageIO.read(inputStream);
        File file = new File(dest);
        ImageIO.write(bufferedImage, "jpg", file);
        inputStream.close();
        System.out.println("success");
    }
	/**
	* 以字节的方式将文件以JPG格式写入到本地
	*/
    public void toJpg(byte[] bytes, String dest) throws IOException {

        InputStream inputStream = new ByteArrayInputStream(bytes);
        BufferedImage bufferedImage = ImageIO.read(inputStream);

        // create a blank, RGB, same width and height, and a white background
        BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
                bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
        newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);

        // write to jpeg file
        ImageIO.write(newBufferedImage, "jpg", new File(dest));
    }
}


File imgFile=certificateImgForm.getImgFile();//TODO 保存图片信息获取到所在路径
					if(imgFile==null)continue;
					String suffix=imgFile.getName().substring(imgFile.getName().lastIndexOf(".")+1);
					String filepath=BusinessConstant.QUALIFICATION_UPLOAD+linkmanPkid+File.separator+imgFile.getName();
					File file=new File(filepath);
					if(!file.exists()){
						file.mkdirs();
					}
					InputStream imgIs = new FileInputStream(imgFile);
					BufferedImage bufferedImage = ImageIO.read(imgIs);
			        ImageIO.write(bufferedImage, suffix, file);
			        imgIs.close();


File imgFile=new File("D:\\Documents\\Pictures\\1.jpg");
		String fileName = imgFile.getName();
		System.out.println(fileName);
		fileName = new String (fileName.getBytes("ISO-8859-1"),"UTF-8");
		String suffix=fileName.substring(fileName.lastIndexOf(".")+1);
		String filepath=BusinessConstant.QUALIFICATION_REQUEST_URL+1002+File.separator+(new SimpleDateFormat("yyyyMMddHHmmssSSS")).format(new Date())+"."+suffix;
		File file=new File(BusinessConstant.UPLOAD_ROOT_PATH+filepath);
		if(!file.exists()){
			file.mkdirs();
		}
		InputStream imgIs = new FileInputStream(imgFile);
		BufferedImage bufferedImage = ImageIO.read(imgIs);
	    ImageIO.write(bufferedImage, suffix, file);
	    if(imgIs!=null){
	    	imgIs.close();
		} 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值