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()

最新文章

  1. IIS7.5+WebConfig实现页面伪静态和301重定向
  2. Microsoft Softwares
  3. LeetCode Shortest Word Distance
  4. abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized?
  5. Java [leetcode 7] Reverse Integer
  6. MongoVUE 如何导出数据
  7. Android问题-selection contains a component,button7,introduced in an ancestor and cannot be deleted.
  8. OpenCV Mat 类型定义和赋值
  9. 生成dll文件的示例
  10. net user命令
  11. 关于Adobe Flash 11.3 引起的火狐使用问题
  12. 初学github
  13. c#获取数组中最大的元素
  14. JS运算符的一些简单练习和应用
  15. TreeSet(一)--排序
  16. Django学习日记01_环境搭建
  17. ajaxFileUpload onchang上传文件插件第二次失效刷新一次才能再次调用触发change事件
  18. 快速实现抖音的分享&登录(android)
  19. 带你玩转Visual Studio——带你理解微软的预编译头技术
  20. Linux 正则表达式详解

热门文章

  1. Web UI开发速速种草—Kendo UI for jQuery网格编辑操作概述
  2. ASP.Net参数传递小结
  3. 模块讲解---numpymo模块,matplotlib模块,pandas模块
  4. mysql 执行sql语句执行问题
  5. CF70E Information Reform
  6. Mongodb账户管理
  7. About IndexDB
  8. sz/rz
  9. webpack+vue+Eslint+husky+lint-staged 统一项目编码规范
  10. thinkphp5 大量数据批量插入数据库的解决办法