Java客户端通过Http发送POST请求上传文件到web服务器
http://www.cnblogs.com/WilliamJiang/archive/2012/04/29/2475883.html
1.朋友的一个需求,让我给他实现,需求是这样的,需要用ASP.net写一个页面负责处理客户端上传的文件,并根据传递的参数把文件保存到相应的目录。客户端是手机应用程序,因为没学过Android,所以我只是写了一个Java的Demo用来上传文件。
服务端:

public partial class _Default : System.Web.UI.Page
{ private string id = "";
private string userName = "";
private string type = "";
private string fileName = "";
//文件长度
private long contentLength = 0;
private static readonly string filePath = ConfigurationManager.AppSettings["filePath"];
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
id = Request["id"];
userName = Request["user"];
type = Request["type"];
fileName = Request.Headers["FileName"];
writeFile();
}
} /// <summary>
/// 上传文件
/// </summary>
private void writeFile()
{
try
{
Stream stream = Request.InputStream;
contentLength = stream.Length;
string currentFilePath = filePath + userName;
if (!Directory.Exists(currentFilePath))
{
Directory.CreateDirectory(currentFilePath);
} FileStream fileStream = File.Create(currentFilePath + @"\" + fileName);
//每次读取的1024个字节
byte[] bytes = new byte[1024]; int numReadByte = 0;
while ((numReadByte = stream.Read(bytes, 0, 1024)) != 0)
{
fileStream.Write(bytes, 0,numReadByte);
}
//关闭流
stream.Close();
fileStream.Close(); }

Java文件上传客户端示例,(几年没搞java有点生疏了):

import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.HttpURLConnection;
import java.net.URL; /**
*
* 只是写的一个示例,filePath,和FileName根据需要进行调整。
*/
public class MyTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub String str="http://localhost:2906/Default.aspx?id=1&user=2&type=3";
String filePath="D:\\Wildlife.wmv";
String fileName="Wildlife.wmv";
try {
URL url=new URL(str);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.addRequestProperty("FileName", fileName);
connection.setRequestProperty("content-type", "text/html");
BufferedOutputStream out=new BufferedOutputStream(connection.getOutputStream()); //读取文件上传到服务器
File file=new File(filePath);
FileInputStream fileInputStream=new FileInputStream(file);
byte[]bytes=new byte[1024];
int numReadByte=0;
while((numReadByte=fileInputStream.read(bytes,0,1024))>0)
{
out.write(bytes, 0, numReadByte);
}
out.flush();
fileInputStream.close();
//读取URLConnection的响应
DataInputStream in=new DataInputStream(connection.getInputStream());
} catch (Exception e) {
e.printStackTrace();
} } }
Java客户端通过Http发送POST请求上传文件到web服务器的更多相关文章
- python发送post请求上传文件,无法解析上传的文件
前言 近日,在做接口测试时遇到一个奇葩的问题. 使用post请求直接通过接口上传文件,无法识别文件. 遇到的问题 以下是抓包得到的信息: 以上请求是通过Postman直接发送请求的. 在这里可以看到消 ...
- libcurl 上传文件至 web服务器
测试环境搭建, 使用 wamp server (windows下的 apache+MySQL+php) libcurl vc6 工程代码 下载地址: http://download.csdn.ne ...
- SpringMVC实现PUT请求上传文件
在JQuery中,我们可以进行REST ful中delete和put的请求,但是在java EE标准中,默认只有在POST请求的时候,servlet 才会通过getparameter()方法取得请求体 ...
- SSM框架下,使用ajax请求上传文件(doc\docx\excel\图片等)
1.准备工作 1.1.添加上传必要jar包 <dependency> <groupId>commons-io</groupId> <artifactId> ...
- Postman Post请求上传文件
Postman Post请求上传文件一.选择post请求方式,输入请求地址 二.填写Headers Key:Content-Type :Value:multipart/form-data 如下图 三. ...
- java 上传文件到 ftp 服务器
1. java 上传文件到 ftp 服务器 package com.taotao.common.utils; import java.io.File; import java.io.FileInpu ...
- python中使用multipart/form-data请求上传文件
最近测试的接口是上传文件的接口,上传单个文件,我主要使用了2种方法~ 接口例如: URL: http://www.baidu.com/*** method:post 参数: { "salar ...
- SpringBoot 上传文件到linux服务器 异常java.io.FileNotFoundException: /tmp/tomcat.50898……解决方案
SpringBoot 上传文件到linux服务器报错java.io.FileNotFoundException: /tmp/tomcat.50898-- 报错原因: 解决方法 java.io.IOEx ...
- element-ui上传组件,通过自定义请求上传文件
记录使用element-ui上传组件,通过自定义请求上传文件需要注意的地方. <el-upload ref="uploadMutiple" :auto-upload=&quo ...
随机推荐
- 2016年10月11日 星期二 --出埃及记 Exodus 18:22
2016年10月11日 星期二 --出埃及记 Exodus 18:22 Have them serve as judges for the people at all times, but have ...
- wireshark抓包直观图解 TCP三次握手/四次挥手详解
转http://www.seanyxie.com/category/linux/ 作者:seanyxie | 一. TCP/IP协议族 TCP/IP是一个协议族,通常分不同层次进行开发,每个层次负 ...
- HTML DOM简介
HTML DOM简介 1.当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model),HTML DOM模型被创建为对象的树.如下所示: 2.通过可编程的对象模型,Ja ...
- 使用mysql数据库,插入数据出现问号(?)的问题,解决方法
首先,我用的mysql数据库是5.7.12版本. 出现的问题: 1.插入数据显示错误,插入不成功,出现:Incorrect string value: '\xCD\xF5\xD5\xBC\xBE\xA ...
- BZOJ 3546 Life of the Party (二分图匹配-最大流)
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3546 题意:给定一个二分图.(AB两个集合的点为n,m),边有K个.问去掉哪些点后 ...
- BZOJ 3674: 可持久化并查集加强版
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3674 题意:三种操作:(1)合并ab所在集合:(2)查询ab是否在一个集合:(3) ...
- BZOJ 2879 美食节(费用流-动态加边)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2879 题意:有n道菜,每道菜需要b[i]份,m个厨师,第j个厨师做第i道菜需要时间a[i ...
- Problem W UVA 662 二十三 Fast Food
Fast Food Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status P ...
- __declspec关键字详细用法
__declspec关键字详细用法 __declspec用于指定所给定类型的实例的与Microsoft相关的存储方式.其它的有关存储方式的修饰符如static与extern等是C和C++语言的ANSI ...
- linux tar命令
tar命令打包还是压缩需要看所调用的命令参数....tar在使用时可以调用命令参数, 比如tar -xvf +文件名就是解包,但是不是解压...只有在使用了参数z等调用gzip等 压缩命令时才是压缩或 ...