/** * 下载文件 * * @return */ public void download() { request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); try { request.setCharacterEncoding("UTF-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } session = request.getSession(); // 业务编号 String businessNUM = request.getParameter(ParamsKeyUtil.URL_PARAMS_KEY_BUSINESSNUM); // 获取client HttpRequestSender httpRequestSender = new HttpRequestSenderImpl(); HttpClient client = null; client = httpRequestSender.getHttpClient(request); if (client == null) { this.addActionMessage("登录已超时,请重新登录"); msg = "timeout"; } else { // 获取URL URLService urlService = new URLServiceImpl(); String url = urlService.getURL(request, businessNUM); String filename = request.getParameter("fileName"); String filepath = FilePathConfigUtil.getValue("oa.filePath") + filename; if (httpRequestSender.fromRemoteDownloadFile(client, url, filepath)) { File downloadFile = new File(filepath); if (downloadFile.exists()) { response.setContentType("application/octet-stream"); Long length = downloadFile.length(); response.setContentLength(length.intValue()); response.addHeader("Content-Disposition", "attachment; filename=" + filename); try { FileInputStream inputStream = new FileInputStream(filepath); ServletOutputStream sopt = response.getOutputStream(); int b = 0; byte[] buffer = new byte[1024]; while (b != -1) { b = inputStream.read(buffer); // 4.写到输出流(out)中 sopt.write(buffer, 0, b); } inputStream.close(); sopt.close(); sopt.flush(); } catch (IOException e) { e.printStackTrace(); } } else { } } } }
/** * 上传文件 * * @return */ public void upload() { request = ServletActionContext.getRequest(); session = request.getSession(); String filepath = FilePathConfigUtil.getValue("oa.filePath") + getImageFileName(); String filename = getImageFileName(); String index = request.getParameter("index"); // 业务编号 String businessNUM = request.getParameter(ParamsKeyUtil.URL_PARAMS_KEY_BUSINESSNUM); // 获取client HttpRequestSender httpRequestSender = new HttpRequestSenderImpl(); HttpClient client = httpRequestSender.getHttpClient(request); if (uploadFile(filepath)) { if (client == null) { this.addActionMessage("登录已超时,请重新登录"); msg = "timeout"; } else { // 获取URL URLService urlService = new URLServiceImpl(); String url = urlService.getURL(request, businessNUM); // 请求 String htmlsource = httpRequestSender.uploadFileToRemote(client, url, filepath, filename, index); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/xml;charset=utf-8"); response.setCharacterEncoding("UTF-8"); response.setHeader("Cache-Control", "no-cache"); PrintWriter out = null; try { out = response.getWriter(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } out.println(htmlsource); out.flush(); out.close(); } } }
// 访问 private Map<String, Object> remote(String businessNUM, HttpClient client) { // 获取URL URLService urlService = new URLServiceImpl(); String url = urlService.getURL(request, businessNUM); // 请求 HttpRequestSender httpRequestSender = new HttpRequestSenderImpl(); String htmlsource = httpRequestSender.getMethodResuest(client, url); // 获取数据并跳转 ServiceUtil serviceUtil = new ServiceUtil(); ParserHtmlSourceService parser = serviceUtil.getPraserService(businessNUM); RemoteUtil remoteUtil = new RemoteUtil(); boolean isremote = remoteUtil.isRemoteSucc(htmlsource); if (remoteUtil.isRemoteSucc(htmlsource) == false) { map = new HashMap<String, Object>(); map.put("msg", "timeout"); } else { map = parser.parserSource(htmlsource); } return map; }
/** ajax请求 获取地址信息 */ public void noLoginAjax() { String htmlSource = ""; request = ServletActionContext.getRequest(); session = request.getSession(); // 业务编号 String businessNUM = request.getParameter(ParamsKeyUtil.URL_PARAMS_KEY_BUSINESSNUM); // 获取client HttpRequestSender httpRequestSender = new HttpRequestSenderImpl(); HttpClient client = httpRequestSender.getHttpClient(request); if (client == null) { this.addActionMessage("登录已超时,请重新登录"); } else { // 获取URL URLService urlService = new URLServiceImpl(); String url = urlService.getURL(request, businessNUM); // 请求 htmlSource = httpRequestSender.getMethodResuest(client, url); } ajaxResponse(htmlSource); }