java本地下载上传图片_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java本地下载上传图片

java本地下载上传图片

 2015/1/23 15:25:17  andy_angel  程序员俱乐部  我要评论(0)
  • 摘要:一、本地的一个下载url图片并上传在本地//生成图片路径,图片类型protectedStringchangeImagePath(Stringimagepath,StringcreateDir,ImgImagesimgImages,intsiteId){Stringpath=imagepath;intindex=path.lastIndexOf("/");//图片名+后缀StringimageNameAndEndsWith=path.substring(index+1,path.length()
  • 标签:图片 上传 Java 下载
一、本地的一个下载url图片并上传在本地
class="java">//生成图片路径,图片类型
	protected String changeImagePath(String imagepath, String createDir, ImgImages imgImages,int siteId) {
		String path = imagepath;
		int index = path.lastIndexOf("/");
		//图片名+后缀
        String imageNameAndEndsWith = path.substring(index+1, path.length());
        imgImages.setOriginalName(imageNameAndEndsWith);
        //图片名称
        String imageName = imageNameAndEndsWith.substring(0, imageNameAndEndsWith.lastIndexOf("."));
        imgImages.setImgName(imageName);
        String imageEndsWith = imageNameAndEndsWith.substring(imageNameAndEndsWith.lastIndexOf("."), imageNameAndEndsWith.length());
        
        //图片类型
        String imageType = imageEndsWith.substring(imageEndsWith.lastIndexOf(".")+1, imageEndsWith.length());
        imgImages.setImgType(imageType);
        //判断是否包含指定的后缀
        if (!imgsEndsWith.contains(imageEndsWith.toLowerCase())) {
        	if (logger.isDebugEnabled()) {
				logger.debug("Image suffix is invalid");
			}
        	throw new RuntimeException("Image suffix is invalid");
        }
       
        //Date date = new Date();
	    //SimpleDateFormat fs = new SimpleDateFormat("yyyy/M/d");
	    //String formatDate = fs.format(date);
        //开始拼写图片库中图片路径格式  如:/img/2013/3/7/25/6618496.jpg
        Calendar calendar = Calendar.getInstance();
	    StringBuilder strBuilder = new StringBuilder();
	    //strBuilder.append(createDir);//图片目录
	    strBuilder.append("/");
	    strBuilder.append(calendar.get(Calendar.YEAR));//年
	    strBuilder.append("/");
	    strBuilder.append(siteId);//站点id
	    strBuilder.append("/");
	    int month  = calendar.get(Calendar.MONTH)+1;
	    strBuilder.append(month);//月
	    strBuilder.append("/");
	    strBuilder.append(calendar.get(Calendar.DAY_OF_MONTH));//日
	    strBuilder.append("/");
	    //开始创建目录
	    File file = new File(createDir+strBuilder.toString());
	    if (!file.exists()) {
	    	file.mkdirs();
	    }
	    strBuilder.append(imgImages.getId()-1);//图片名称为图片id-1
	    //strBuilder.append(UUID.randomUUID());
	    strBuilder.append(imageEndsWith);//图片后缀格式
	    imgImages.setImgPath(strBuilder.toString());
	    
		return strBuilder.toString();
	}

//下载图片
protected File imageDownload(String interUrl, String newImgPath) throws IOException {
		OutputStream os = null;
		InputStream is = null;
		File outFile = null;
		try{
			URL url = new URL(interUrl);
			outFile = new File(newImgPath);
			os = new FileOutputStream(outFile);
			is = url.openStream();
			byte[] buff = new byte[9024];
			//从指定 url下载图片
			while(true) {
				int readed = is.read(buff);
				if(readed == -1) {
					break;
				}
				byte[] temp = new byte[readed];
				System.arraycopy(buff, 0, temp, 0, readed);
				os.write(temp);
			}
			return outFile;
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					if (logger.isDebugEnabled()) {
						logger.debug("InputStream close exception ...");
					}
				}
			}
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					if (logger.isDebugEnabled()) {
						logger.debug("OutputStream close exception ...");
					}
				}
			}
		}
	}

//获取图片大小,尺寸
	protected void getImageProerty(File picture, ImgImages imgImages) throws FileNotFoundException, IOException {
        BufferedImage sourceImg;
		sourceImg = ImageIO.read(new FileInputStream(picture.getAbsolutePath()));
		int imageSize = (int)(picture.length()/1024);  
        int imageWidth = sourceImg.getWidth();  
        int imageHeight = sourceImg.getHeight();
        //sourceImg.flush();
        imgImages.setImgSize(imageSize);
        imgImages.setImgWidth(imageWidth);
        imgImages.setImgHeight(imageHeight);
	}
发表评论
用户名: 匿名