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年11月9日 星期三 --出埃及记 Exodus 19:25
2016年11月9日 星期三 --出埃及记 Exodus 19:25 So Moses went down to the people and told them.于是摩西下到百姓那里告诉他们.
- shell脚本之lnmp的搭建
!/bin/bash #this script is source packages installed lnmp .xmal yum -y install wget #"========= ...
- Spring中MultipartHttpServletRequest实现文件上传
Spring中MultipartHttpServletRequest实现文件上传 转贴自:http://my.oschina.net/nyniuch/blog/185266 实现图片上传 用户必须能 ...
- #查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素
#!/usr/bin/env python #查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素. # li = ["alec", &quo ...
- C#多线程之二:ManualResetEvent和AutoResetEvent
初次体验 ManualResetEvent和AutoResetEvent主要负责多线程编程中的线程同步:以下一段是引述网上和MSDN的解析: 在.Net多线程编程中,AutoResetEvent和Ma ...
- CodeForces 490C Hacking Cypher
Hacking Cypher Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Sub ...
- jquery之each()、$.each()[jQuery.each()]
导航: 1,jQuery对象(实例)的each()函数 2,全局jQuery对象的each()函数 一:jQuery对象(实例)的each()函数 each()函数用于以当前jQuery对象匹配到的每 ...
- [UVA11464]Even Parity(状压,枚举)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- linux下inotify的使用
有时候我们需要检测某个目录下文件或者子目录的改动状况,如添加.删除.以及更新等,Linux系统上提供了inotify来完成这个功能.inotify是在版本2.6.13的内核中首次出现,现在的发行本应该 ...
- cert