两个服务器之间文件传递(JAVA)

书读的少文笔不好,废话就不多说了。(首次发文,如有不对或是代码有漏洞,请及时指出,万分感谢)!!!
书读的少文笔不好,废话就不多说了。(首次发文,如有不对或是代码有漏洞,请及时指出,万分感谢)!!!
书读的少文笔不好,废话就不多说了。(首次发文,如有不对或是代码有漏洞,请及时指出,万分感谢)!!!
</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 = (HttpURLConnection) 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">
  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值