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(" "); } }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()