当前位置: 代码迷 >> Web前端 >> WebOffice 开发文档-code
  详细解决方案

WebOffice 开发文档-code

热度:567   发布时间:2012-10-24 14:15:58.0
WebOffice 开发文档--code
	/**
	 * 初始化附件编辑页面
	 */
	private void initOnlineEdit(HttpServletRequest request,
			HttpServletResponse response, AttachmentVO attachment, String uploadPath)
			throws ApplicationException {
		FileInputStream fileInputStream = null;
		BufferedInputStream bufferedInputStream = null;
		BufferedOutputStream bufferedOutputStream = null;
		try {
			// 获取输入输出流
			fileInputStream = new FileInputStream(new File(uploadPath));
			bufferedInputStream = new BufferedInputStream(fileInputStream);
			bufferedOutputStream = new BufferedOutputStream(response
					.getOutputStream());

			response.setContentType("application/x-download");
			response.setHeader("Content-disposition", "attachment; filename="
					+ attachment.getOverview());

			byte[] buffer = new byte[1024];
			int readBytes = 0;
			while ((readBytes = bufferedInputStream.read(buffer, 0,
					buffer.length)) != -1) {
				bufferedOutputStream.write(buffer, 0, readBytes);
			}
			bufferedOutputStream.flush();
		} catch (IOException e) {
			throw new ApplicationException(e);
		} catch (Exception e) {
			throw new ApplicationException(e);
		} finally {
			try {
				fileInputStream.close();
				bufferedInputStream.close();
				bufferedOutputStream.close();
			} catch (IOException e) {
				throw new ApplicationException(e);
			}
		}
	}
	
	/**
	 * 编辑后上传处理
	 */
	private int uploadAttachment(HttpServletRequest request,
			HttpServletResponse response, AttachmentVO attachment, String dir) throws ApplicationException {
		// 获取上传路径
		String uploadPath = dir + File.separator + attachment.getImageUrl()
				+ File.separator;
		try {
			PageContext pageContext = JspFactory.getDefaultFactory()
					.getPageContext(this.getServlet(), request, response, null,
							true, 8192, true);

			SmartUpload smartUpload = new SmartUpload();
			smartUpload.initialize(pageContext);
			smartUpload.upload();
			
			return smartUpload.save(uploadPath);
		} catch (ServletException e) {
			throw new ApplicationException(e);
		} catch (SmartUploadException e) {
			throw new ApplicationException(e);
		} catch (IOException e) {
			throw new ApplicationException(e);
		}
	}
}
  相关解决方案