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 就更有用了

最新文章

  1. dev中控件属性设置
  2. javascript 之注意url特殊字符限制
  3. js 实现list类
  4. position绝对剧中
  5. 【Python开发实战】Python环境的配置
  6. JAVA程序生成XML标准化的文件格式,缩进,美化。
  7. Python - 字母算术谜题
  8. 专业辟谣----ThinkSNS不仅仅是微博程序!
  9. [Android]上传到多个Maven仓库的Gradle插件RapidMavenPushPlugin
  10. linux TCP数据包封装在SKB的过程分析
  11. 动态将ASPX生成HTML网页并将网页导出PDF
  12. 安装部署jumpserver3.0
  13. 用php实现斐波那契数列,如: 1, 1, 2, 3, 5, 8, 13, 21, 34。求出第20个数的值。
  14. NET 使用 RabbitMQ
  15. Python经典算法片段
  16. python 多线程和多进程
  17. Redis缓存使用技巧
  18. OpenGL ES 2.0 Shader 调试新思路(一): 改变提问方式
  19. java中获取文件目录
  20. sql语句 case when then else end 语句实例

热门文章

  1. quick 3.3载入Spine问题
  2. hdu 4324 Triangle LOVE(拓扑判环)
  3. Armbian hostname and WiFi configuration
  4. TFRecord —— tensorflow 下的统一数据存储格式
  5. 2015合肥网络赛 HDU 5492 Find a path 动归
  6. CentOS 源设置
  7. Linux 终端仿真程序Putty
  8. 在AT151上面测试串口通讯
  9. P2186 小Z的栈函数
  10. Nginx模型 & 惊群问题