- 浏览: 235057 次
文章分类
最新评论
-
iwangyiyx2:
...
加盐算法 -
seffxu:
我安装了run-jetty-run插件,并配置完成;通过Deb ...
jetty eclipse 插件 run-jetty-run
package com.cn.sevlet; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.UnknownServiceException; import java.util.HashMap; import java.util.Map; import java.util.Set; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.InputStreamEntity; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import android.content.Context; public class ClientBuilder { private static final String CHARSET = "utf-8"; @SuppressWarnings("null") public static String request(Context context, String uri, UnamParameters param, String httpMethod) throws UnsupportedEncodingException, IllegalStateException, IOException { String result = null; HttpClient client = new DefaultHttpClient(); if (httpMethod.equalsIgnoreCase("get")) { BufferedReader reader = null; HttpGet get = new HttpGet(uri); HttpResponse response = client.execute(get); reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), CHARSET));//地址 StringBuffer sb = new StringBuffer(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line); } reader.close(); result = sb.toString(); return result; } else if (httpMethod.equalsIgnoreCase("post")) { final String MULTIPART_FORM_DATA = "multipart/form-data"; final String BOUNDARY = "infovillage";//分隔符 final String TWO_HYPHENS = "--"; final String ENDLINE = System.getProperty("line.separator"); Map<String, String> table = new HashMap<String, String>(); table.put("product", param.getProduct()); table.put("price", param.getPrice()); table.put("unit", param.getUnit()); table.put("name", param.getName()); table.put("telephone", param.getTelephone()); table.put("address", param.getAddress()); table.put("description", param.getDescription()); StringBuffer sb = new StringBuffer(); Set<Map.Entry<String, String>> set = table.entrySet(); /*文件内容为二进制流的形式*/ for (Map.Entry<String, String> entry : set) { sb.append(TWO_HYPHENS + BOUNDARY + ENDLINE); sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + ENDLINE); sb.append(ENDLINE); sb.append(entry.getValue() + ENDLINE); } sb.append(TWO_HYPHENS + BOUNDARY + ENDLINE); sb.append("Content-Disposition: form-data; name=\"picture\"; filename=\"uploadImage.jpg\"" + ENDLINE); sb.append("Content-Type: image/jpeg" + ENDLINE); sb.append(ENDLINE); sb.append(param.getPicture()); sb.append(ENDLINE); sb.append(TWO_HYPHENS + BOUNDARY + TWO_HYPHENS +ENDLINE); HttpPost post = new HttpPost(uri); post.addHeader("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY); DataOutputStream output=null; try{ output.writeBytes(sb.toString()); }catch(IOException e){ e.printStackTrace(); } ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); swapStream.writeTo(output); byte []bytes= swapStream.toByteArray(); InputStream in = new ByteArrayInputStream(bytes); output.flush(); post.setEntity(new InputStreamEntity(in, bytes.length)); // System.out.println(sb.toString()); HttpResponse response = client.execute(post); BufferedReader reader = null; reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer strbuf = new StringBuffer(); String line = null; while((line = reader.readLine()) != null) { strbuf.append(line); } result = strbuf.toString(); reader.close(); } return result; } public static InputStream getFile(Context context, String uri, UnamParameters param, String httpMethod) throws UnknownServiceException { InputStream is = null; if (httpMethod.equalsIgnoreCase("GET")) { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(uri); try { HttpResponse response = client.execute(get); is = response.getEntity().getContent(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return is; }
发表评论
-
Hudson问题汇总
2014-01-14 16:58 710混淆部分不识别嵌入class,在新文件中重新定义cl ... -
lenovo 后台访问方法
2013-07-22 10:23 0relayserver服务器也在249 ... -
lenovo日常记录(命令,脚本等)
2013-07-22 10:22 0mount -o remount,rw /syste ... -
AnyShare团队微分享——AR(增强现实)
2013-06-18 17:47 0问题: 1、什么是AR(图像增强)? 2、AR有哪 ... -
android AP介绍 和接口使用
2012-11-16 11:18 4399以下基于android ics系统 Android A ... -
一个变态的统计文件中log不重复字符的个数的程序
2012-11-15 00:17 978package com.xiaxing.io; i ... -
加盐算法
2012-10-23 11:04 10192附加(目前MD5,还有用RSA,速度稍慢,安全性更好) ... -
近距离传感器 (proximity sensor )
2012-10-19 17:02 27243近距离传感器的作用 ... -
java内存分析
2012-10-09 16:55 1042实时的工具有profilling/monitoring等,实时 ... -
代码心得
2012-08-14 14:03 850这次解决数据打包的问题,由于别人的代码错误导致程序关联性崩溃。 ... -
apache DataOutputStream 用法
2012-08-09 23:12 11class http { Htt ... -
一段关于HttpURLConnection的demo
2012-08-09 20:57 1818package test; import java ...
相关推荐
Apache Commons Compress库中的BZip2Stream和BZip2Compressor类提供了与BZ2文件交互的能力。通过这些类,我们可以读取BZ2压缩文件的内容,也可以将数据压缩成BZ2格式。例如,我们可以使用BZip2...
在Java中,我们可以使用内置的`BufferedReader`类配合`split()`方法来读取CSV文件,但更常见的是使用第三方库如Apache Commons CSV或OpenCSV。以下是一个使用Apache Commons CSV库的示例: ```java import org....
response.getOutputStream().write(buffer, 0, length); } } catch (IOException e) { // 处理异常 } ``` 4. 关闭流:确保所有的输入/输出流在操作完成后都被正确关闭。 总结,Apache Servlet 提供了处理HTTP...
define('FOPEN_READ_WRITE_CREATE_DESTRUCTIVE','w b'); // truncates existing file data, use with care define('FOPEN_WRITE_CREATE','ab'); define('FOPEN_READ_WRITE_CREATE','a b'); define('FOPEN...
$ready_streams = stream_select($read_streams, $write_streams, $except_streams, $timeout); if ($ready_streams === false) { echo "Error occurred during stream selection.\n"; break; } elseif ($ready...
response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); try (InputStream in = new FileInputStream(fileToDownload);...
din.read(tableStream); din.close(); // ... 处理表格中的文本 ... StringBuffer sb = new StringBuffer(); for (int x = 0; x (); x++) { WordTextPiece nextPiece = (WordTextPiece) text.get(x); sb....
1. **写与追尾读(Write & Tailing Read)**:生产者将消息写入Pulsar,消费者则从消息的末尾开始读取,即所谓的“追尾读”。这种方式适用于实时性要求较高的场景,例如实时数据分析或监控系统等。 2. **追赶读与...
ImageIO.write(image, "JPEG", new File("D:/output_" + page + ".jpg")); } document.close(); ``` 2. **JPG转PDF**: 对于JPG到PDF的转换,我们可以使用Java的ImageIO类读取JPG图片,然后利用PDFBox创建一个...
imageStream.read(imageData); imageStream.close(); ``` 接下来,创建一个`Picture`对象,并将其添加到`Drawing`对象中: ```java import org.apache.poi.ss.usermodel.Picture; import org.apache.poi.xssf....
while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } } ``` 这里的关键在于设置正确的MIME类型(`application/octet-stream`),以及在响应头中添加`Content-Disposition`来指定文件名,...
out.write(buffer, 0, bytes_read); } catch (Exception e) { System.err.println(e); System.err.println("Usage: java GetURL <URL> []"); } finally { // 无论如何都要关闭流 try { in.close(); out....
Apache Hudi 学习指南 Apache Hudi 是一个开源的数据管理系统,旨在处理大规模的分析数据集。它提供了流式处理能力,能够实时...5. Read / Write Optimized:Hudi 可以根据实际情况优化读写操作,提高数据处理效率。
These well-established and time-honored rules have made it easier for nations and people to live and work together. Part of protocol has always been the acknowledgment of the hierarchical standing of...
os.write(buffer, 0, read); } os.flush(); os.close(); bis.close(); fis.close(); ``` 在实际项目中,为了保证文件安全性和性能,通常会结合使用这些方法。例如,对于小文件,可以使用直接输出的方式;对于大...
imageStream.read(imageData); imageStream.close(); XSSFPictureData pictureData = ppt.addPicture(imageData, XSLFRelation.PICTURE.getType()); XDDFShape pictureShape = slide.createDrawingPatriarch()....
while ((length = fis.read(buffer)) != -1) { response.getOutputStream().write(buffer, 0, length); } fis.close(); } else { response.getWriter().write("文件不存在!"); } } } ``` 在这个例子中,...
os.write(buffer, 0, read); } os.close(); is.close(); // 处理成功后的逻辑,如跳转到成功页面 return mapping.findForward("success"); } } ``` 4. **文件下载**: 文件下载通常涉及到生成HTTP响应,...