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