/** * Send a HTTP POST request to the specified URL * * @param url Target endpoint URL * @param headers Any HTTP headers that should be added to the request * @param payload Content payload that should be sent * @param contentType Content-type of the request * @return Returned HTTP response * @throws IOException If an error occurs while making the invocation */ public HttpResponse doPost(String url, final Map<String, String> headers, final String payload, String contentType) throws IOException { HttpUriRequest request = new HttpPost(url); setHeaders(headers, request); HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request; final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING)); EntityTemplate ent = new EntityTemplate(new ContentProducer() { public void writeTo(OutputStream outputStream) throws IOException { OutputStream out = outputStream; if (zip) { out = new GZIPOutputStream(outputStream); } out.write(payload.getBytes()); out.flush(); out.close(); } }); ent.setContentType(contentType); if (zip) { ent.setContentEncoding("gzip"); } entityEncReq.setEntity(ent); return client.execute(request); }
/** * Send a HTTP PATCH request to the specified URL * * @param url Target endpoint URL * @param headers Any HTTP headers that should be added to the request * @param payload Content payload that should be sent * @param contentType Content-type of the request * @return Returned HTTP response * @throws IOException If an error occurs while making the invocation */ public HttpResponse doPatch(String url, final Map<String, String> headers, final String payload, String contentType) throws IOException { HttpUriRequest request = new HttpPatch(url); setHeaders(headers, request); HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request; final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING)); EntityTemplate ent = new EntityTemplate(new ContentProducer() { public void writeTo(OutputStream outputStream) throws IOException { OutputStream out = outputStream; if (zip) { out = new GZIPOutputStream(outputStream); } out.write(payload.getBytes()); out.flush(); out.close(); } }); ent.setContentType(contentType); if (zip) { ent.setContentEncoding("gzip"); } entityEncReq.setEntity(ent); return client.execute(request); }
/** * Send a HTTP PUT request to the specified URL * * @param url Target endpoint URL * @param headers Any HTTP headers that should be added to the request * @param payload Content payload that should be sent * @param contentType Content-type of the request * @return Returned HTTP response * @throws IOException If an error occurs while making the invocation */ public HttpResponse doPut(String url, final Map<String, String> headers, final String payload, String contentType) throws IOException { HttpUriRequest request = new HttpPut(url); setHeaders(headers, request); HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request; final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING)); EntityTemplate ent = new EntityTemplate(new ContentProducer() { public void writeTo(OutputStream outputStream) throws IOException { OutputStream out = outputStream; if (zip) { out = new GZIPOutputStream(outputStream); } out.write(payload.getBytes()); out.flush(); out.close(); } }); ent.setContentType(contentType); if (zip) { ent.setContentEncoding("gzip"); } entityEncReq.setEntity(ent); return client.execute(request); }
public void handle( final HttpRequest request, final HttpResponse response, final HttpContext context) throws HttpException, IOException { final String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH); if (!method.equals("GET") && !method.equals("HEAD")) { throw new MethodNotSupportedException(method + " method not supported"); } final EntityTemplate body = new EntityTemplate(new ContentProducer() { public void writeTo(final OutputStream outstream) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); writer.write(mJSON); writer.flush(); } }); response.setStatusCode(HttpStatus.SC_OK); body.setContentType("text/json; charset=UTF-8"); response.setEntity(body); }
public void handle(HttpRequest request, HttpResponse response, HttpContext arg2) throws HttpException, IOException { if (request.getRequestLine().getMethod().equals("POST")) { // Retrieve the POST content HttpEntityEnclosingRequest post = (HttpEntityEnclosingRequest) request; byte[] entityContent = EntityUtils.toByteArray(post.getEntity()); String content = new String(entityContent, Charset.forName("UTF-8")); // Execute the request final String json = RequestHandler.handle(content); // Return the response EntityTemplate body = new EntityTemplate(new ContentProducer() { public void writeTo(final OutputStream outstream) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); writer.write(json); writer.flush(); } }); response.setStatusCode(HttpStatus.SC_OK); body.setContentType("application/json; charset=UTF-8"); response.setEntity(body); } }
/** * Wraps an XML writing into a ContentProducer for * lazy serialization. * @param doc the XML document to wrap. * @return The ContentProducer. */ public static ContentProducer toContentProducer(final Document doc) { return out -> { try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, UTF8); transformer.setOutputProperty(OutputKeys.INDENT, "no"); transformer.transform( new DOMSource(doc), new StreamResult(out)); } catch (TransformerException te) { throw new IOException(te); } }; }
/** * Send a HTTP POST request to the specified URL * * @param url Target endpoint URL * @param headers Any HTTP headers that should be added to the request * @param payload Content payload that should be sent * @param contentType Content-type of the request * @return Returned HTTP response * @throws IOException If an error occurs while making the invocation */ public HttpResponse doPost(String url, final Map<String, String> headers, final String payload, String contentType) throws IOException { HttpUriRequest request = new HttpPost(url); setHeaders(headers, request); HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request; final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING)); EntityTemplate ent = new EntityTemplate(new ContentProducer() { public void writeTo(OutputStream outputStream) throws IOException { OutputStream out = outputStream; if (zip) { out = new GZIPOutputStream(outputStream); } out.write(payload.getBytes(Charset.defaultCharset())); out.flush(); out.close(); } }); ent.setContentType(contentType); if (zip) { ent.setContentEncoding("gzip"); } entityEncReq.setEntity(ent); return client.execute(request); }
/** * Send a HTTP PUT request to the specified URL * * @param url Target endpoint URL * @param headers Any HTTP headers that should be added to the request * @param payload Content payload that should be sent * @param contentType Content-type of the request * @return Returned HTTP response * @throws IOException If an error occurs while making the invocation */ public HttpResponse doPut(String url, final Map<String, String> headers, final String payload, String contentType) throws IOException { HttpUriRequest request = new HttpPut(url); setHeaders(headers, request); HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request; final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING)); EntityTemplate ent = new EntityTemplate(new ContentProducer() { public void writeTo(OutputStream outputStream) throws IOException { OutputStream out = outputStream; if (zip) { out = new GZIPOutputStream(outputStream); } out.write(payload.getBytes(Charset.defaultCharset())); out.flush(); out.close(); } }); ent.setContentType(contentType); if (zip) { ent.setContentEncoding("gzip"); } entityEncReq.setEntity(ent); return client.execute(request); }
/** * Send a HTTP OPTIONS request to the specified URL * * @param url Target endpoint URL * @param headers Any HTTP headers that should be added to the request * @param payload Content payload that should be sent * @param contentType Content-type of the request * @return Returned HTTP response * @throws IOException If an error occurs while making the invocation */ public HttpResponse doOptions(String url, final Map<String, String> headers, final String payload, String contentType) throws IOException { HttpUriRequest request = new HttpOptions(url); setHeaders(headers, request); if(payload != null) { HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request; final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING)); EntityTemplate ent = new EntityTemplate(new ContentProducer() { public void writeTo(OutputStream outputStream) throws IOException { OutputStream out = outputStream; if (zip) { out = new GZIPOutputStream(outputStream); } out.write(payload.getBytes()); out.flush(); out.close(); } }); ent.setContentType(contentType); if (zip) { ent.setContentEncoding("gzip"); } entityEncReq.setEntity(ent); } return client.execute(request); }
protected ContentProducer newContentProducer(String contentType, List docs) { if ("text/csv".equals(contentType)) { return new CsvContentProducer(docs); } else if (PIPELINE_DOC_TYPE.equals(contentType) || "application/json".equals(contentType)) { return new JacksonContentProducer(jsonObjectMapper, docs); } else { throw new IllegalArgumentException("Content type "+contentType+" not supported! Use text/csv or "+PIPELINE_DOC_TYPE); } }
private ContentProducer toApacheContentProducer(RepeatableContentProducer repeatableContentProducer) { return (OutputStream outputStream) -> { try (InputStream inputStream = repeatableContentProducer.getInputStream()) { copyLarge(inputStream, outputStream); } }; }
@Override public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException { String contentType; Integer code; Uri uri = Uri.parse(request.getRequestLine().getUri()); String fileUri = URLDecoder.decode(uri.getLastPathSegment()); final byte[] r; byte[] resp; AssetManager mgr = context.getAssets(); try { resp = Utility.loadInputStreamAsByte(mgr.open(fileUri)); contentType = Utility.getMimeTypeForFile(fileUri); code = HttpStatus.OK.getCode(); } catch (IOException e){ resp = Utility.loadInputStreamAsByte(mgr.open("notfound.html")); contentType = Utility.MIME_TYPES.get("html"); code = HttpStatus.NOT_FOUND.getCode(); } r=resp; HttpEntity entity = new EntityTemplate(new ContentProducer() { public void writeTo(final OutputStream outstream) throws IOException { outstream.write(r); } }); ((EntityTemplate)entity).setContentType(contentType); response.setStatusCode(code); response.setEntity(entity); }
@Override public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException { String contentType; Integer code; Uri uri = Uri.parse(request.getRequestLine().getUri()); String folder; List<String> s = uri.getPathSegments(); String path = File.separator; for(int i=1;i<s.size()-1;i++) { path+=s.get(i)+File.separator; } folder = path; final File file = new File(Utility.BLACKHOLE_PATH +folder+s.get(s.size()-1)); final byte[] r; byte[] resp; if(file.exists()){ resp = Utility.loadFileAsByte(file.getAbsolutePath()); contentType = Utility.getMimeTypeForFile(file.getName()); code = HttpStatus.OK.getCode(); }else{ AssetManager mgr = context.getAssets(); resp = Utility.loadInputStreamAsByte(mgr.open("notfound.html")); contentType = Utility.MIME_TYPES.get("html"); code = HttpStatus.NOT_FOUND.getCode(); } r=resp; HttpEntity entity = new EntityTemplate(new ContentProducer() { public void writeTo(final OutputStream outstream) throws IOException { outstream.write(r); } }); ((EntityTemplate)entity).setContentType(contentType); response.setStatusCode(code); response.addHeader(HttpHeader.CONTENT_DISPOSITION, "attachment"); response.setEntity(entity); }
public HttpEntity forgeResponse(){ HttpEntity entity = new EntityTemplate(new ContentProducer() { public void writeTo(final OutputStream outstream) throws IOException { outstream.write(response); } }); ((EntityTemplate)entity).setContentType(contentType); return entity; }
protected ContentProducer newContentProducer(String contentType, List docs) { if ("text/csv".equals(contentType)) { return new CsvContentProducer(docs); } else if (JSON_CONTENT_TYPE.equals(contentType) || PIPELINE_DOC_CONTENT_TYPE.equals(contentType)) { return new JacksonContentProducer(jsonObjectMapper, docs); } else { throw new IllegalArgumentException("Content type "+contentType+ " not supported! Use text/csv, application/json, or "+PIPELINE_DOC_CONTENT_TYPE); } }
private EntityTemplate createEntity() { return new EntityTemplate(new ContentProducer() { public void writeTo(OutputStream outputStream) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8"); writer.write(payload); writer.flush(); } }); }
/** * Update (overwrite) the metadata of the resource identified by the given uri. The metadata will be serialised to * application/json and sent to the Apache Marmotta server. The given metadata will override any metadata * for the resource already existing on the server. The resource has to exist or be created before updating, otherwise * the method will throw a NotFoundException. * * @param uri the URI of the resource to update * @param metadata the metadata to upload to the resource * @throws IOException * @throws MarmottaClientException */ public void updateResourceMetadata(final String uri, final Metadata metadata) throws IOException, MarmottaClientException { HttpClient httpClient = HTTPUtil.createClient(config); HttpPut put = new HttpPut(getServiceUrl(uri)); put.setHeader(CONTENT_TYPE, "application/rdf+json; rel=meta"); ContentProducer cp = new ContentProducer() { @Override public void writeTo(OutputStream outstream) throws IOException { RDFJSONParser.serializeRDFJSON(ImmutableMap.of(uri, metadata), outstream); } }; put.setEntity(new EntityTemplate(cp)); try { HttpResponse response = httpClient.execute(put); switch(response.getStatusLine().getStatusCode()) { case 200: log.debug("metadata for resource {} updated",uri); break; case 415: log.error("server does not support metadata type application/json for resource {}, cannot update", uri); throw new ContentFormatException("server does not support metadata type application/json for resource "+uri); case 404: log.error("resource {} does not exist, cannot update", uri); throw new NotFoundException("resource "+uri+" does not exist, cannot update"); default: log.error("error updating resource {}: {} {}",new Object[] {uri,response.getStatusLine().getStatusCode(),response.getStatusLine().getReasonPhrase()}); throw new MarmottaClientException("error updating resource "+uri+": "+response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase()); } } catch (UnsupportedEncodingException e) { log.error("could not encode URI parameter",e); throw new MarmottaClientException("could not encode URI parameter"); } finally { put.releaseConnection(); } }
private void uploadDataset(final InputStream in, final String mimeType, HttpClient httpClient) throws IOException, URISyntaxException { HttpPost post = HTTPUtil.createPost(URL_UPLOAD_SERVICE, config); post.setHeader(CONTENT_TYPE, mimeType); ContentProducer cp = new ContentProducer() { @Override public void writeTo(OutputStream outstream) throws IOException { ByteStreams.copy(in,outstream); } }; post.setEntity(new EntityTemplate(cp)); ResponseHandler<Boolean> handler = new ResponseHandler<Boolean>() { @Override public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException { EntityUtils.consume(response.getEntity()); switch(response.getStatusLine().getStatusCode()) { case 200: log.debug("dataset uploaded updated successfully"); return true; case 412: log.error("mime type {} not acceptable by import service",mimeType); return false; default: log.error("error uploading dataset: {} {}",new Object[] {response.getStatusLine().getStatusCode(),response.getStatusLine().getReasonPhrase()}); return false; } } }; try { httpClient.execute(post, handler); } catch(IOException ex) { post.abort(); throw ex; } finally { post.releaseConnection(); } }
/** * 以POST方式请求(上传JSON串) * * @param url * 接口地址 * @param requestBody * 发送的请求数据包 JSON串 * @return Message对象 */ public static Message doPost(String url, final String requestBody) { Log.i(TAG, "POST:REQUEST URL IS --- " + url); HttpPost httpPost = new HttpPost(url); ContentProducer contentProducer = new ContentProducer() { public void writeTo(OutputStream outstream) throws IOException { Writer writer = new OutputStreamWriter(outstream, ENCODEING); writer.write(requestBody); writer.flush(); } }; HttpEntity entity = new EntityTemplate(contentProducer); httpPost.setEntity(entity); return executeRequest(httpPost); }
public void postDocsToPipeline(String hostAndPort, String pipelinePath, List docs, int requestId, String contentType) throws Exception { FusionSession fusionSession = getSession(hostAndPort, requestId); String postUrl = hostAndPort + pipelinePath; if (postUrl.indexOf("?") != -1) { postUrl += "&echo=false"; } else { postUrl += "?echo=false"; } HttpPost postRequest = new HttpPost(postUrl); ContentProducer cp = newContentProducer(contentType, docs); EntityTemplate et = new EntityTemplate(cp); et.setContentType(contentType); et.setContentEncoding(StandardCharsets.UTF_8.name()); postRequest.setEntity(et); HttpEntity entity = null; CloseableHttpResponse response = null; try { HttpClientContext context = null; if (isKerberos) { response = httpClient.execute(postRequest); } else { context = HttpClientContext.create(); if (cookieStore != null) { context.setCookieStore(cookieStore); } response = httpClient.execute(postRequest, context); } entity = response.getEntity(); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 401) { // unauth'd - session probably expired? retry to establish log.warn("Unauthorized error (401) when trying to send request " + requestId + " to Fusion at " + hostAndPort + ", will re-try to establish session"); // re-establish the session and re-try the request EntityUtils.consumeQuietly(entity); entity = null; response.close(); synchronized (this) { fusionSession = resetSession(hostAndPort); if (fusionSession == null) throw new IllegalStateException("After re-establishing session when processing request " + requestId + ", hostAndPort " + hostAndPort + " is no longer active! Try another hostAndPort."); } log.info("Going to re-try request " + requestId + " after session re-established with " + hostAndPort); if (isKerberos) { response = httpClient.execute(postRequest); } else { response = httpClient.execute(postRequest, context); } entity = response.getEntity(); statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200 || statusCode == 204) { log.info("Re-try request " + requestId + " after session timeout succeeded for: " + hostAndPort); } else { raiseFusionServerException(hostAndPort, entity, statusCode, response, requestId); } } else if (statusCode != 200 && statusCode != 204) { raiseFusionServerException(hostAndPort, entity, statusCode, response, requestId); } else { // OK! if (fusionSession != null && fusionSession.docsSentMeter != null) fusionSession.docsSentMeter.mark(docs.size()); } } finally { EntityUtils.consumeQuietly(entity); if (response != null) response.close(); } }
@Override public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException { String contentType = Utility.MIME_TYPES.get("html"); Integer code; final byte[] r; String resp; AssetManager mgr = context.getAssets(); try { resp = Utility.loadInputStreamAsString(mgr.open("index.html")); code = HttpStatus.OK.getCode(); } catch (IOException e){ resp = Utility.loadInputStreamAsString(mgr.open("notfound.html")); code = HttpStatus.NOT_FOUND.getCode(); } final ArrayList<FileL> fl = new ArrayList<>(); Uri uri = Uri.parse(request.getRequestLine().getUri()); String folder="/"; if(uri.getPath().contains("~")) { List<String> s = uri.getPathSegments(); String path = File.separator; for(int i=1;i<s.size()-1;i++) { path+=s.get(i)+File.separator; } if(s.size()>1) { fl.add(new FileL("..", "/~" + path, "-", "DIR")); folder = path + s.get(s.size() - 1) + File.separator; }else{ folder = path; } } File f = new File(Utility.BLACKHOLE_PATH +folder); if(f.isDirectory()) { File[] files = f.listFiles(); for (File inFile : files) { if (inFile.isDirectory()) { fl.add(new FileL(inFile.getName(),"/~"+folder+inFile.getName(),"-","DIR")); } else { fl.add(new FileL(inFile.getName(),"/file"+folder+inFile.getName(),Utility.getFileSize(inFile))); } } } final String wfolder = folder; Template tmpl = Mustache.compiler().compile(resp); r=tmpl.execute(new Object() { Object filelist = fl; String title = context.getString(R.string.app_title); String wd = wfolder; }).getBytes(); HttpEntity entity = new EntityTemplate(new ContentProducer() { public void writeTo(final OutputStream outstream) throws IOException { outstream.write(r); } }); ((EntityTemplate)entity).setContentType(contentType); response.setStatusCode(code); response.setEntity(entity); }
/** * Update the content of the resource identified by the URI given as argument. The resource has to exist before * content can be uploaded to it. Any existing content will be overridden. The stream of the content object * will be consumed by this method. Throws ContentFormatException if the content type is not supported, * NotFoundException if the resource does not exist. * @param uri * @param content * @throws IOException * @throws MarmottaClientException */ public void updateResourceContent(final String uri, final Content content) throws IOException, MarmottaClientException { HttpClient httpClient = HTTPUtil.createClient(config); HttpPut put = new HttpPut(getServiceUrl(uri)); put.setHeader(CONTENT_TYPE, content.getMimeType() + "; rel=content"); ContentProducer cp = new ContentProducer() { @Override public void writeTo(OutputStream outstream) throws IOException { ByteStreams.copy(content.getStream(),outstream); } }; put.setEntity(new EntityTemplate(cp)); ResponseHandler<Boolean> handler = new ResponseHandler<Boolean>() { @Override public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException { EntityUtils.consume(response.getEntity()); switch(response.getStatusLine().getStatusCode()) { case 200: log.debug("content for resource {} updated",uri); return true; case 406: log.error("server does not support content type {} for resource {}, cannot update", content.getMimeType(),uri); return false; case 404: log.error("resource {} does not exist, cannot update", uri); return false; default: log.error("error updating resource {}: {} {}",new Object[] {uri,response.getStatusLine().getStatusCode(),response.getStatusLine().getReasonPhrase()}); return false; } } }; try { httpClient.execute(put, handler); } catch(IOException ex) { put.abort(); throw ex; } finally { put.releaseConnection(); } }
/** * Update the configuration "key" with the given value. Value can be either a list of values or one of the * primitive types String, Boolean, Integer, Double * @param key * @param value * @throws IOException * @throws MarmottaClientException */ public void setConfiguration(String key, final Object value) throws IOException, MarmottaClientException { HttpClient httpClient = HTTPUtil.createClient(config); String serviceUrl = config.getMarmottaUri() + URL_CONFIG_SERVICE + "/data/" + URLEncoder.encode(key,"utf-8"); HttpPost post = new HttpPost(serviceUrl); post.setHeader(CONTENT_TYPE, "application/json"); ContentProducer cp = new ContentProducer() { @Override public void writeTo(OutputStream outstream) throws IOException { ObjectMapper mapper = new ObjectMapper(); if(value instanceof Collection) { mapper.writeValue(outstream,value); } else { mapper.writeValue(outstream, Collections.singletonList(value.toString())); } } }; post.setEntity(new EntityTemplate(cp)); try { HttpResponse response = httpClient.execute(post); switch(response.getStatusLine().getStatusCode()) { case 200: log.debug("configuration {} updated successfully",key); break; case 404: log.error("configuration with key {} does not exist",key); throw new NotFoundException("configuration with key "+key+" does not exist"); default: log.error("error updating configuration {}: {} {}",new Object[] {key,response.getStatusLine().getStatusCode(),response.getStatusLine().getReasonPhrase()}); throw new MarmottaClientException("error updating configuration "+key+": "+response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase()); } } finally { post.releaseConnection(); } }
@Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { if (!Config.ALLOW_DOWNLOAD) { response.setStatusCode(HttpStatus.SC_SERVICE_UNAVAILABLE); response.setEntity(ViewFactory.getSingleton().renderTemp(request, "503.html")); return; } HttpGetParser parser = new HttpGetParser(); Map<String, String> params = parser.parse(request); String fname = params.get("fname"); Header referer = request.getFirstHeader("Referer"); if (fname == null || referer == null) { response.setStatusCode(HttpStatus.SC_BAD_REQUEST); return; } fname = URLDecoder.decode(fname, Config.ENCODING); String refPath = new URL(URLDecoder.decode(referer.getValue(), Config.ENCODING)).getPath(); final File file; if (refPath.equals("/")) { file = new File(this.webRoot, fname); } else if (!refPath.startsWith(this.webRoot)) { response.setStatusCode(HttpStatus.SC_FORBIDDEN); response.setEntity(ViewFactory.getSingleton().renderTemp(request, "403.html")); return; } else { file = new File(refPath, fname); } final String encoding = isGBKAccepted(request) ? "GBK" : Config.ENCODING; HttpEntity entity = new EntityTemplate(new ContentProducer() { @Override public void writeTo(OutputStream outstream) throws IOException { if (file.isFile()) { write(file, outstream); } else { zip(file, outstream, encoding); } } }); response.setStatusCode(HttpStatus.SC_OK); response.addHeader("Content-Description", "File Transfer"); response.setHeader("Content-Type", "application/octet-stream"); response.addHeader("Content-Disposition", "attachment;filename=" + encodeFilename(file)); response.setHeader("Content-Transfer-Encoding", "binary"); // 在某平板自带浏览器上下载失败,比较下能成功下载的响应头,这里少了Content-Length。但设了,反而下不了了。 response.setEntity(entity); }