上传、下载压缩图片

/**
	 * 
	 * @param uploadImage --本地路径(tomacat路径)
	 * @param serverDir   --服务器路径
	 * @param imageType   --文件或图片类型
	 * 此方法可以上传文件或图片.txt,.jpg,.gif等
	 */
	public void upload(String uploadImage,String serverDir,String imageType){
		FileInputStream fis=null;
		FileOutputStream fos=null;
		
		//服务器路径
		File serverFile=new File(serverDir);
		if(!serverFile.exists()){
			serverFile.mkdir();
		}
		String serverPath=separator+BbsUtil.getCurrentMonth()+imageType;
		serverFile=new File(serverDir+serverPath);
		try {
//				if(!serverFile.exists()){
//						serverFile.createNewFile();
//				}	
				fis=new FileInputStream(new File(uploadImage));
				fos=new FileOutputStream(serverFile);
				byte[] buffer = new byte[1024];
				int len = 0;
					while ((len = fis.read(buffer)) > 0) {
						fos.write(buffer, 0, len);
					}
					fos.flush();
		} catch (Exception e) {
				e.printStackTrace();
		}finally{
				try {
					fos.close();
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
	/**
	 * 
	 * @param path 本地路径
	 * @param serverDir  服务器目录
	 * @param serverFile 服务器文件
	 * @param smallSize  文件大小
	 * @param middleSize  。。。
	 * @param maxSize     。。。
	 * @return
	 */
	public boolean compressImage(File path,String serverDir,String serverFile,String smallSize, String middleSize,String maxSize){
		String fileName=path.getName();
		String msg="";
		if(!path.canRead()){
			msg="图片"+fileName+"不可读!";
			throw new NullPointerException(msg);
		}
		File serverPathFile=new File(serverDir);
		if(!serverPathFile.exists()){
			serverPathFile.mkdir();
		}
		
		FileOutputStream out = null;// 文件输出流
		int width=0;
		int height=0;
		BufferedImage tag=null;
		try{
				/* ============= 中图 处理 ==================  */
				// 建立输出流
				out = new FileOutputStream(serverDir+separator+serverFile );
				out.flush();
				// 依照固定大小画图区域300*300
				width = Integer.parseInt(middleSize);
				height = width;
				Image src = javax.imageio.ImageIO.read(path);
				tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
				tag.getGraphics().drawImage(src, 0, 0, width, height, null);
				// 画图操作
				JPEGCodec.createJPEGEncoder(out).encode(tag);
				out.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return true;
	}
	public String getImageType(String path){
		if(path.lastIndexOf(".")==-1){
			new NullPointerException("上传文档格式不正确!");
		}
		String imageType=path.substring(path.lastIndexOf("."),path.length());
		return imageType;
	}
///////////////////////////下载功能、、、、、、、、、、、、、、、、、
	public String  downloadFile(){
		String filename = getHttpServletRequest().getParameter("filename");//"1a.txt";
		String filepath = getHttpServletRequest().getParameter("filepath");//"d:\\";
		try {
			filename=new String(filename.getBytes("ISO8859-1"),"utf-8").replaceAll("%20", " ");
			filepath=new String(filepath.getBytes("iso8859-1"),"utf-8").replaceAll("%20", " ");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		try {
			if (filename == null || filepath == null || "".equals(filename) || "".equals(filepath)) {
				System.out.println("找不到要下载的文件!");
			} else {
				String fullpath = filepath +File.separator+filename;
				File file = new File(fullpath);
				if (file == null || !file.exists()) {
					System.out.println("找不到要下载的文件!");
				} else {	
					
					getHttpServletResponse().setContentType("application/octet-stream");
					//解决中文乱码\不直接打开文件
					getHttpServletResponse().setHeader("Content-Disposition", "attachment;filename = " + new String(filename.getBytes("gb2312"),"ISO8859-1" ));
					BufferedInputStream input = new BufferedInputStream(new FileInputStream(fullpath));
					byte buffBytes[] = new byte[1024];
					int allLength = 0;
					System.out.println("下载文件路径:" + fullpath);
					System.out.println("开始下载");
					OutputStream os = getHttpServletResponse().getOutputStream();
					System.out.println("获得流之前");
					int read = 0;
					while ((read = input.read(buffBytes)) != -1) {
						allLength += read;
						os.write(buffBytes, 0, read);
					}
					System.out.println("获得流之后:" + allLength);
					os.flush();
					os.close();
					input.close();
					System.out.println("正常关闭文件流");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return null;
	}	

你可能感兴趣的:(下载)