httpurlconnection发送文件到服务端并接收
httpurlconnection发送文件到服务端并接收
客户端
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/*
* @author xk
* 上传文件到文件服务器的客户端
*/
public class FileUploadClient{
public static void main(String[] args) {
try {
// URL url = new URL("http://127.0.0.1:8888/fileSys/getFile.action?filePath=/c/v/b/&fileName=test.zip");
URL url = new URL("http://127.0.0.1:8888/fileSys/getFile.action?filePath=/c/v/b/&fileName=test.zip");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// 发送POST请求必须设置如下两行 conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","text/html");
conn.setRequestProperty("Cache-Control","no-cache");
conn.setRequestProperty("Charsert", "UTF-8");
conn.connect();
conn.setConnectTimeout();
OutputStream out =conn.getOutputStream(); File file = new File("C:\\fileSys\\test.zip"); DataInputStream in = new DataInputStream(new FileInputStream(file)); int bytes = ;
byte[] buffer = new byte[];
while ((bytes = in.read(buffer)) != -) {
out.write(buffer, , bytes);
}
in.close();
out.flush();
out.close(); conn.getInputStream();
conn.disconnect();
} catch (Exception e) {
System.out.println("发送文件出现异常!" + e);
e.printStackTrace();
}
}
}
服务端
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.property.PropertyUtil; public class GetFileAction extends ActionSupport { private static final long serialVersionUID = -9108080625441081032L;
public String execute() throws Exception
{
String rootPath = PropertyUtil.getProperty("fileSys.properties","fileSys.path"); HttpServletRequest request = ServletActionContext.getRequest();
String filePath = request.getParameter("filePath");
String fileName = request.getParameter("fileName"); InputStream input = request.getInputStream();
String fileFullPath = rootPath + filePath + fileName; File file = new File(rootPath + filePath);
if(!file.exists()){
file.mkdirs();
}
FileOutputStream fos = new FileOutputStream(fileFullPath); int size = ;
byte[] buffer = new byte[];
while ((size = input.read(buffer,,)) != -) {
fos.write(buffer, , size);
}
fos.close();
input.close(); return Action.NONE;
}
}
如果用jersey,形成RESTfull webservices 就更有用了
httpurlconnection发送文件到服务端并接收的更多相关文章
- Wince 6.0适用 .NET 使用HttpRequest的Post上传文件,服务端的Web API接收Post上传上来的文件 代码
//调用的示例 private string fileName = "InStorageData.csv"; string filePath = parentPath + Comm ...
- android上传图片、视频、文件,服务端使用wcf接收
最近一直在搞android上传图片.视频.文件,服务端使用wcf接收,本文对调试中的遇到的问题进行记录. 首先android上传一些小图片是比较容易的一天下来差不多就能调试出来,但是上传一些大的文件时 ...
- 写一个基于TCP协议套接字,服务端实现接收客户端的连接并发
''' 写一个基于TCP协议套接字,服务端实现接收客户端的连接并发 ''' client import socket import time client = socket.socket() clie ...
- asp.net上传Excel文件到服务端进行读取
1.我们IIS是使用7.5,由于在网站中上传Excel文件到服务端进行数据读取时候出现读取失败情况.一开始以为是没有按照office软件问题,其实不然,因为server是64位操作系统,如果我们要使用 ...
- 微信公众号开发 但是服务端仅仅接收到了 p1 p2 没有接收到p3
w 栗子 js window.location = www.xx.com?p1=123&p2=456&p3=789 但是服务端仅仅接收到了 p1 p2 没有接收到p3 原因有哪些呢? ...
- 基于TCP协议套接字,服务端实现接收客户端的连接并发
基于TCP协议套接字,服务端实现接收客户端的连接并发 服务端 import socket from multiprocessing import Process server=socket.socke ...
- httpurlConnection客户端发送文件与服务端接受文件
import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; ...
- android菜鸟学习笔记24----与服务器端交互(一)使用HttpURLConnection和HttpClient请求服务端数据
主要是基于HTTP协议与服务端进行交互. 涉及到的类和接口有:URL.HttpURLConnection.HttpClient等 URL: 使用一个String类型的url构造一个URL对象,如: U ...
- HttpURLConnection实现两个服务端的对接
在企业开发中,很多时候需要用到两个服务端的对接,在java类中进行连接并传递参数,其中的HttpURLConnection是一种轻量化,并且简单的方法! package httptest; impor ...
随机推荐
- 《JSP+Servlet+Tomcat应用开发从零開始学》
当当网页面: http://product.dangdang.com/23619990.html 内容简单介绍 本书全面介绍了 JSP开发中涉及的相关技术要点和实战技巧. 全书结构清晰,难 ...
- android-Animation进阶(创造用户舒服的动画)
android中经常使用的动画有Animation ,Animator两种; ---第1种经常使用的是使用在Activity切换中.比方打开一个Activity.关闭一个Activity 个人比較喜欢 ...
- hdoj--1342--Lotto(dfs)
Lotto Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 2.最详细的WSDD配置文件注释
https://blog.csdn.net/u011063151/article/details/52590282
- Python3小白初体验
三层循环嵌套,以后优化 data = { "北京":{ "a":{ ", " }, "b":{ ", &quo ...
- 4.graph.h
#pragma once #include <stdio.h> #include <graphics.h> #include <mmsystem.h> #pragm ...
- SpringMVC与SpringBoot返回静态页面遇到的问题
1.SpringMVC静态页面响应 package com.sv.controller; import org.springframework.stereotype.Controller; impor ...
- 机器学习实践:《Python机器学习实践指南》中文PDF+英文PDF+代码
机器学习是近年来渐趋热门的一个领域,同时Python 语言经过一段时间的发展也已逐渐成为主流的编程语言之一.<Python机器学习实践指南>结合了机器学习和Python 语言两个热门的领域 ...
- Linux下redis安装(单机版)
redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcached类似,但很大程度补偿了 memcached的不足,它支持存储的value类型相对更多,包括stri ...
- 关于Java IO InputStream 的一点整理!
程序的开发其中一直在用文件的读写.可是对于java其中输入流以及输出流仅仅是会用不理解,一直以来想搞清楚其,可是一直没有运行(悲剧).今天早上抽出半个小时通过JDK API1.6.0中文版帮助逐步的了 ...