我想将原来图片保存在本地功能改成保存到服务器上
String filename = "D:\\ScenePics\\Plate20210416165034669.jpg";
这个路径改写成网络地址
http:\\8.140.107.161\\ScenePics\\Plate20210416165034669.jpg;
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
String newName = sf.format(new Date());
FileOutputStream fout;
try {
// String filename = ".\\pic\\"+ new String(pAlarmer.sDeviceIP).trim() + "_"
// + newName+"_type["+strItsPlateResult.struPicInfo[i].byType+"]_ItsPlate.jpg";
String filename = "D:\\ScenePics\\Plate20210416165034669.jpg";
fout = new FileOutputStream(filename);
//将字节写入文件
long offset = 0;
ByteBuffer buffers = strItsPlateResult.struPicInfo[i].pBuffer.getByteBuffer(offset, strItsPlateResult.struPicInfo[i].dwDataLen);
byte [] bytes = new byte[strItsPlateResult.struPicInfo[i].dwDataLen];
buffers.rewind();
buffers.get(bytes);
fout.write(bytes);
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
首先,不可以通过http协议直接将流写入到网络位置。需要遵守协议的格式。
可以在服务器上开启一个接口,接受你要上传的图片,然后再使用POST请求等,向这个接口发送图片二进制。
也可以使用FTP协议直接上传。
总之使用网络传输数据必须要遵守双方约定好的协议喔。(о´∀`о)