Apache HttpComponents 文件上传例子
/*
* ====================================================================
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/ package org.apache.http.examples.entity.mime; import java.io.File; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils; /**
* Example how to use multipart/form encoded POST request.
*/
public class ClientMultipartFormPost { public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("File path not given");
System.exit(1);
}
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost("http://localhost:8080" +
"/servlets-examples/servlet/RequestInfoExample"); FileBody bin = new FileBody(new File(args[0]));
StringBody comment = new StringBody("A binary file of some kind"); MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", bin);
reqEntity.addPart("comment", comment); httppost.setEntity(reqEntity); System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity(); System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
}
EntityUtils.consume(resEntity);
} finally {
try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
}
} }
Apache HttpComponents 文件上传例子的更多相关文章
- SpringMVC学习总结(五)——SpringMVC文件上传例子
这是用的是SpringMVC-3.1.1.commons-fileupload-1.2.2和io-2.0.1 首先是web.xml <?xml version="1.0" e ...
- apache fileupload 文件上传,及文件进度设置获取
文件上传action处理: boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) ...
- apache 修改文件上传大小限制
Windows 环境下的修改方法 ================================================================ 第一步:修改在php5下POST文件 ...
- struts2 s:file标签使用及文件上传例子
<s:form action="uploadaction" method="post" enctype="multipart/form-da ...
- SpringMVC学习总结(六)——SpringMVC文件上传例子(2)
基本的SpringMVC的搭建在我的上一篇文章里已经写过了,这篇文章主要说明一下使用SpringMVC进行表单上的文件上传以及多个文件同时上传的不同方法 一.配置文件: SpringMVC 用的是 的 ...
- 模拟文件上传(三):使用apache fileupload组件进行文件批量上传
其中涉及到的jar包 jsp显示层: <%@ page language="java" import="java.util.*" pageEncoding ...
- 模拟文件上传(二):使用apache fileupload组件进行文件上传
其中涉及到的jar包: jsp显示层: <%@ page language="java" import="java.util.*" pageEncodin ...
- Spring MVC 之文件上传(七)
SpringMVC同样使用了apache的文件上传组件.所以需要引入以下包: apache-commons-fileupload.jar apache-commons-io.jar 在springAn ...
- PHP漏洞全解(九)-文件上传漏洞
本文主要介绍针对PHP网站文件上传漏洞.由于文件上传功能实现代码没有严格限制用户上传的文件后缀以及文件类型,导致允许攻击者向某个可通过 Web 访问的目录上传任意PHP文件,并能够将这些文件传递给 P ...
随机推荐
- 利用ASP.NET一般处理程序动态生成Web图像(转)
摘自:http://www.cnblogs.com/zhouhb/archive/2011/02/15/1955262.html 一般处理程序的扩展名为ashx,它实现了IHttpHandler接口, ...
- linux账户密码安全策略
前言 对于服务器安全来说,服务器的账号密码是很重要的事情 我们可以选择取消账号密码登陆,只使用公钥登录,但有时可能并不方便 这里告诉大家账号密码如何管理更加安全 一.账号密码最大使用天数 在/etc/ ...
- Oracle分组函数以及数据分组
简单总结一下对于数据的分组和分组函数. 本文所举实例,数据来源oracle用户scott下的emp,dept ,salgrade 3表:数据如下: 一.分组函数 1.sum()求和函数.max()求最 ...
- android调节屏幕亮度
一:只改变当前程序android屏幕亮度(1)方法:lp.screenBrightness 取值 0.0 -- 1.0 ※设定值(float)的范围,默认小于 0(系统设定).0.0(暗)-1.0(亮 ...
- __attribute__中constructor和destructor
1.前言 最近看到一份代码,看到一个函数前面用__attribute__((destructor))修饰,当时感觉有点怪怪的,搜了整个程序,也没发现哪个地方调用这个函数.于是从字面意思猜想,该函数会在 ...
- Spring注解运行时抛出null
关于Spring的注解其实不难,大致需要以下几个流程: 一.配置Spring的注解支持 <?xml version="1.0" encoding="UTF-8&qu ...
- powerdesigner基础操作
一.建表 1. 打开PowerDesigner,点击File->New 2. 选择Conceptual Data Model,并修改Model name. 3. ...
- Oracle监听配置、数据库实例配置等
参考:http://jingyan.baidu.com/article/3aed632e7a638b70108091dd.html linux下面搞Orale参考:http://blog.sina.c ...
- 机器学习(4): KNN 算法
1. 综述 1.1 Cover和Hart在1968年提出了最初的邻近算法 1.2 分类(classification)算法 1.3 输入基于实例的学习(instance- ...
- django web 笔记
安装 django pip install django 创建虚拟环境 python -m venv testenvironment 进入虚拟环境: testenvironment\Script ...