您的位置:首页 > 编程语言 > Java开发

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

2016-05-03 09:38 585 查看
[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">
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息