import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection; import org.apache.commons.lang.StringUtils;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import com.kexion.eagle.common.dao.DaoException;
import com.kexion.ssdr.dmp.web.utils.PropertiesUtil;
//客户端发送
public class TestSendFile { public static void main(String[] args) {
try {
sendFile1("Template_ZYMLBZHJC.xlsx", "D:"+File.separatorChar+"2019"+File.separatorChar+"个人"+File.separatorChar+"Template_ZYMLBZHJC.xlsx", "zxsb");
} catch (Exception e) {
e.printStackTrace();
}
}
private static String sendFile1(String filename,String dir,String type) throws Exception {
PropertiesUtil util = new PropertiesUtil("config/zymlk.properties");
Object obj = util.get(type);
if(obj==null){
throw new Exception("调用省厅接口失败");
}
String actionUrl = (java.lang.String) util.get(type);
if(StringUtils.isEmpty(actionUrl)){
throw new Exception("调用省厅接口失败");
} String u1 = actionUrl+"?filename="+filename;
URL url =new URL(u1);
System.out.println(u1);
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-type", "text/html");
httpURLConnection.setRequestProperty("Cache-Control", "no-cache");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.connect(); OutputStream out = httpURLConnection.getOutputStream();
DataInputStream in = null; File file = new File(dir);
in = new DataInputStream(new FileInputStream(file));
int bytes=0;
byte[] buffer = new byte[1024];
while((bytes=in.read(buffer))!=-1){
out.write(buffer,0,bytes);
}
out.flush(); InputStream inputStream=null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
StringBuffer resultBuffer = null;
if(httpURLConnection.getResponseCode()==HttpURLConnection.HTTP_OK){
inputStream = httpURLConnection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
reader = new BufferedReader(inputStreamReader);
String tmpLine = null;
resultBuffer = new StringBuffer();
while((tmpLine=reader.readLine())!=null){
resultBuffer.append(tmpLine);
resultBuffer.append("\n");
}
}else{
        int code=httpURLConnection.getResponseCode()
        throw new DaoException("上报失败,失败代码["+code+"]");
} in.close();
out.close();
reader.close();
inputStreamReader.close();
inputStream.close();
System.out.println(resultBuffer.toString());
return resultBuffer.toString();
} }

//服务端接收

package com.dd.demo.controller;

import java.io.*;
import java.util.List; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class StbmglController {
//打印日志
private static final Logger logger = LoggerFactory.getLogger(StbmglController.class); @ResponseBody
@RequestMapping("getDsFile1")
public String getDsFile1(HttpServletRequest request,HttpServletResponse response){
logger.info("开始接受文件");
JSONObject result = new JSONObject();
try { String filename = request.getParameter("filename");
logger.info("filename={}",filename);
InputStream input = request.getInputStream();
File getFile = new File("C:\\Users\\Administrator\\Desktop\\"+filename); FileOutputStream fos = new FileOutputStream(getFile);
boolean flag = false;
int size = 0;
byte[] buffer = new byte[1024];
while ((size=input.read(buffer,0,1024))!=-1){
flag = true;
fos.write(buffer,0,size);
}
result.put("success",flag);
} catch (Exception e) {
result.put("success",false);
result.put("msg","接受文件失败");
logger.error("接受文件失败");
e.printStackTrace();
}
return result.toString();
}
}
httpURLConnection.getResponseCode()

httpurlConnection客户端发送文件与服务端接受文件的更多相关文章

  1. android 上传文件用php程序在服务端接受(一)

    php服务端接受程序..file_up.php. <?php /* require_once('lib/session_config.php'); require_once('lib/flydc ...

  2. Java后端HttpClient Post提交文件流 及服务端接收文件流

    客户端将文件转换为流发送: 依赖的包: <dependency> <groupId>org.apache.httpcomponents</groupId> < ...

  3. android 发送GET请求 服务端接收乱码的问题

    在android的编程中常会使用get/post请求,在用get请求的时候数据是直接放在url当中的 例如: http://apicloud.mob.com/v1/weather/query?key= ...

  4. C#中服务端接受前端JSON字符串转换成字典集合

    我们是否可以把从前端接受的JSON字符串转换成字典集合呢? 比如从前端接收:{'size':'10', 'weight':'10kg'} 在服务端转换成:[{size:"10"}, ...

  5. PHP学习笔记——上传文件到服务端的文件夹下

    环境 开发包:appserv-win32-2.5.10 服务器:Apache2.2 数据库:phpMyAdmin 语言:php5,java 平台:windows 10 需求 编写一个PHP脚本页面,可 ...

  6. Eureka客户端续约及服务端过期租约清理源码解析

    在之前的文章:EurekaClient自动装配及启动流程解析中,我们提到了在构造DiscoveryClient时除了包含注册流程之外,还调度了一个心跳线程: scheduler.schedule( n ...

  7. SpringMVC文件上传下载(单文件、多文件)

    前言 大家好,我是bigsai,今天我们学习Springmvc的文件上传下载. 文件上传和下载是互联网web应用非常重要的组成部分,它是信息交互传输的重要渠道之一.你可能经常在网页上传下载文件,你可能 ...

  8. httpurlconnection发送文件到服务端并接收

    httpurlconnection发送文件到服务端并接收 客户端 import java.io.DataInputStream; import java.io.File; import java.io ...

  9. PHP-Socket服务端客户端发送接收通信实例详解

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://fighter.blog.51cto.com/1318618/1533957 So ...

随机推荐

  1. BZOJ2301——莫比乌斯&&整除分块

    题目 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 分析 莫比乌斯经典入门题. (我也刚学,就写 ...

  2. Springboot注解@ServletComponentScan和@ComponentScan(转)

    一.SpringBoot中使用Servlet在SpringBootApplication上使用@ServletComponentScan注解后,Servlet.Filter.Listener可以直接通 ...

  3. 边学边体验django--HttpRequest 对象

    每个view函数的第一个参数是一个HttpRequest对象. HttpRequest对象包含当前请求URL的一些信息: 属性 描述 path 请求页面的全路径,不包括域名'/hello/' meth ...

  4. 将vim打造成python开发工具

    1.创建vim插件工作目录 [root@ray ~]# mkdir -p ~/.vim/bundle 2.下载插件 [root@ray ~]# cd ~/.vim/bundle [root@ray b ...

  5. 路由器配置——基于链路的OSPF的MD5口令认证

    一.实验目的:掌握基于链路的OSPFMD5口令认证 二.拓扑图: 三.具体步骤配置: (1)R1路由器的配置 Router>enable Router#configure terminal En ...

  6. 浅谈神经网络中的bias

    1.什么是bias? 偏置单元(bias unit),在有些资料里也称为偏置项(bias term)或者截距项(intercept term),它其实就是函数的截距,与线性方程 y=wx+b 中的 b ...

  7. Echarts——关系图(人民的名义为例,简化)源码

    参考博文:https://www.cnblogs.com/emrys5/p/echart-relationship-map.html <!DOCTYPE html> <html> ...

  8. codeforces gym #101161E - ACM Tax(lca+主席树)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: 给出节点数为$n$的树 有$q$次询问,输出$a$节点到$b$节点路程中,经过的边的中位数 ...

  9. 初次接触webpack

    1.学习地址 中文文档 https://www.webpackjs.com/concepts/ webpack-dev-server配置说明 https://www.webpackjs.com/con ...

  10. 学完微型服务器(Tomcat)对其工作流程的理解,自己着手写个简单的tomcat

    学完微型服务器(Tomcat)对其工作流程的理解,自己着手写个简单的tomcat 2019-05-09   19:28:42 注:项目(MyEclipse)创建的时候选择:Web Service Pr ...