两个服务器之间文件传递(JAVA)
2016-05-03 09:38
585 查看
[align=left]书读的少文笔不好,废话就不多说了。(首次发文,如有不对或是代码有漏洞,请及时指出,万分感谢)!!![/align]
[align=left]书读的少文笔不好,废话就不多说了。(首次发文,如有不对或是代码有漏洞,请及时指出,万分感谢)!!![/align]
[align=left]书读的少文笔不好,废话就不多说了。(首次发文,如有不对或是代码有漏洞,请及时指出,万分感谢)!!![/align]
[align=left]书读的少文笔不好,废话就不多说了。(首次发文,如有不对或是代码有漏洞,请及时指出,万分感谢)!!![/align]
[align=left]书读的少文笔不好,废话就不多说了。(首次发文,如有不对或是代码有漏洞,请及时指出,万分感谢)!!![/align]
</pre><span style="color:#000000;"></span><pre class="java" name="code"><span id="_xhe_cursor"> </span>
<span> </span>
<span>//服务器发送端 (注:服务器的环境都是windows Server+tomcat7)</span>
<pre class="java" name="code"><span style="font-size:14px;color:#333333;">package com.xxx.util; import org.apache.log4j.Logger; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class FileUtil { /** * Logger for this class */ private static final Logger logger = Logger.getLogger(FileUtil.class); /** * 将文件流发送至另外服务器的方法(这里有个fileName * 本来是想将文件名放在流里面一起带过去的后来出现问题,如果有朋友知道在这种方法里面怎么把fileName 传过去,麻烦告知一下,万分感谢) * * @param bytes * @param fileName * @return 从服务器端 响应的流 可通过 new String(bytes); 转换 */ public byte[] httpPost(byte[] bytes, String fileName) { try { String url = ""; URL console = new URL(url); HttpURLConnection conn = (HttpURLConnectio 4000 n) console .openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setUseCaches(false); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); conn.connect(); DataOutputStream out = new DataOutputStream(conn.getOutputStream()); out.write(bytes); // 刷新、关闭 out.flush(); out.close(); InputStream is = conn.getInputStream(); if (is != null) { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != -1) { outStream.write(buffer, 0, len); } is.close(); return outStream.toByteArray(); } } catch (Exception e) { e.printStackTrace(); logger.info("文件发送失败++++++++++++++++++++++++++"); } return null; } /** * 将文件转换成byte[] * * @param filePath * @return */ public byte[] getBytes(String filePath) { byte[] buffer = null; try { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); byte[] b = new byte[1024]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } catch (Exception e) { e.printStackTrace(); } return buffer; } public static void main(String[] args) { String filePath="d:/demo/demo.zip"; FileUtil fileUtil=new FileUtil(); byte[] bytes = fileUtil.getBytes(filePath); fileUtil.httpPost(bytes, filePath); } }</span>
</pre><pre class="java" name="code">
//服务器接收端
<pre class="java" name="code"><span style="font-size:14px;color:#666666;">package com.xxx.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.UUID; import javax.servlet.ServletException; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; public class FileReceiveServlet extends HttpServlet{ /** * Logger for this class */ private static final Logger logger = Logger.getLogger(FileReceiveServlet.class); private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { fileReceive(req,resp); } private void fileReceive(HttpServletRequest req, HttpServletResponse resp) { logger.info("接收文件+++++++++++++++++++"); try { //由于此处文件名称没有带过来,只能自己重新定义文件名称 String fileName = "d:/airpxml/"+UUID.randomUUID().toString()+".zip"; ServletInputStream in = req.getInputStream(); File f = new File(fileName); FileOutputStream fos = new FileOutputStream(f); byte[] b = new byte[1024]; int n=0; while((n=in.read(b))!=-1){ fos.write(b,0,n); } fos.close(); in.close(); } catch (Exception e) { logger.info("接收失败+++++++++++++++++"+e.getMessage()); e.printStackTrace(); } } } </span>
</pre><pre class="java" name="code">
相关文章推荐
- java-用HttpURLConnection发送Http请求.
- Mysql IO 内存方面的优化
- Node.js 的异步 IO 性能探讨
- Java Servlet生成JSON格式数据并用jQuery显示的方法
- jsp和servlet中实现页面跳转的方式实例总结
- java servlet结合mysql搭建java web开发环境
- Servlet与JSP间的两种传值情况
- JSP、Servlet中get请求和post请求的区别总结
- jsp和servlet的区别探讨
- SQL Server误区30日谈 第22天 资源调控器可以调控IO
- mysql 数据同步 出现Slave_IO_Running:No问题的解决方法小结
- java中的Io(input与output)操作总结(一)
- Struts2访问servlet分享
- java中Servlet处理乱码的方法
- GO语言的IO方法实例小结
- java中的Io(input与output)操作总结(四)
- Servlet 过滤器详细介绍
- C#路径,文件,目录及IO常见操作汇总
- JSP+Servlet+JavaBean实现登录网页实例详解