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 ...
随机推荐
- wpfのpack协议
当引用的资源需要做成dll时,要用此协议 协议:pack:// 授权:有两种.一种用于访问编译时已经知道的文件,用application:///.一种用于访问编译时不知道.运行时才知道的文件 ...
- NSIS学习记录の----win8.1和win10对于NSIS创建的卸载快捷方式无法在开始目录下显示
NSIS提供了很好的软件卸载功能编写的方法,但是针对win8.1和win10操作系统,由于开始目录的权限限制,我们有时候并不能完美的完成所需要的功能----卸载程序的快捷方式不能显示.话不多说,下面提 ...
- 2016年10月28日 星期五 --出埃及记 Exodus 19:13
2016年10月28日 星期五 --出埃及记 Exodus 19:13 He shall surely be stoned or shot with arrows; not a hand is to ...
- 苹果Xcode帮助文档阅读指南
文档导读 https://developer.apple.com/legacy/library/navigation/ 前面我们讲Xcode的文档结构是在介绍如何能够快速定位到你要找的内容.但是很多人 ...
- Java多个数字求和输出
设计思想: 首先为了能够输入数字,先引入一个类import java.util.Scanner;因为是多个数字求和,所以为了方便声明一个数组double[] num;再声明一个变量int number ...
- SignalR记录
服务端检索数据库,有跟新,推送给客户端 1: GlobalHost.ConnectionManager.GetHubContext<tvHub>().Clients.Client(Clie ...
- HDU 5831 Rikka with Parenthesis II(六花与括号II)
31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- POJ 1142 Smith Numbers(史密斯数)
Description 题目描述 While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Leh ...
- jsp get方式请求参数中包含中文乱码问题解决
1. 自己接收到参数之后在后台进行转码处理 2: 修改tomcat的配置文件 server.xml <Connector port="8080" protocol=&quo ...
- 2013/7/16 HNU_训练赛4
CF328B Sheldon and Ice Pieces 题意:给定一个数字序列,问后面的数字元素能够组成最多的组数. 分析:把2和5,6和9看作是一个元素,然后求出一个最小的组数就可以了. #in ...