URLConnection远程服务调用

 

String uploadUrl="http://localhost:7001/gmap/saveFile.do";
InputStream in = item.getInputStream();
URL url = new URL(uploadUrl);
URLConnection urlConn = url.openConnection();
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
//将信息写入URLConnection 中的OutputStream
OutputStream out = new BufferedOutputStream(urlConn.getOutputStream());
byte[] fileNameBytes = fileName.getBytes("utf-8");
int fileNameLen = fileNameBytes.length;
DataOutputStream dout = new DataOutputStream(out);
dout.writeInt(fileNameLen);
dout.flush();
out.write(fileNameBytes);
int len = 0;
byte[] buf = new byte[512];
while ((len = in.read(buf)) > 0) {
	out.write(buf, 0, len);
}
out.close();
in.close();
InputStream urlIn = urlConn.getInputStream(); while ((len = urlIn.read(buf)) > 0) { System.out.println(new String(buf, 0, len)); } urlIn.close();

 

 

InputStream in = new BufferedInputStream(request.getInputStream());
DataInputStream din = new DataInputStream(in);
int fileNameLen = din.readInt();
byte[] fileNameBytes = new byte[fileNameLen];
in.read(fileNameBytes);
String fileName = new String(fileNameBytes, "utf-8");
//判断当前文件名是否存在
File file = new File(filePath);
if(!file.exists()){
	byte[] buf = new byte[512];
	int len = 0;
	OutputStream out = new BufferedOutputStream(new FileOutputStream(filePath));
	try {
		while ((len = in.read(buf)) > 0) {
			out.write(buf, 0, len);
		}
		out.close();
		message = "文件读取成功!";
	}catch(Exception e){
		message = "文件[" + fileName + "]写失败!";
	}
	finally {
		in.close();
	}
}else{
	message = "文件[" + fileName + "]已存在!";
}
PrintWriter out = res.getWriter();
out.println(message);
out.close();

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值