java如何区分Inputstream里的文件和数据?
接到一个项目,跟对方用http方式通讯,对方发送文件和数据过来,我用InputStream接收,收到详细内容(content-Type:multipart/form-da...
接到一个项目,跟对方用http方式通讯,对方发送文件和数据过来,我用InputStream接收,收到详细内容(content-Type:multipart/form-data等基本信息),肉眼看是能看出哪些是数据,哪些是文件内容,但用代码怎么实现读取所需的数据和文件呢
展开
展开全部
java通过InputStream读取和写入文件操作实例代码
1. File to InputStream
File file = new File("file.xml");
InputStream inputStream = new FileInputStream(file);
2.InputStream to File
InputStream inputStream = new FileInputStream("file.xml");
OutputStream outputStream = new FileOutputStream("file-new.xml");
int bytesWritten = 0;
int byteCount = 0;
byte[] bytes = new byte[1024];
while ((byteCount = inputStream.read(bytes)) != -1)
{
outputStream.write(bytes, bytesWritten, byteCount);
bytesWritten += byteCount;
}
inputStream.close();
outputStream.close();
1. File to InputStream
File file = new File("file.xml");
InputStream inputStream = new FileInputStream(file);
2.InputStream to File
InputStream inputStream = new FileInputStream("file.xml");
OutputStream outputStream = new FileOutputStream("file-new.xml");
int bytesWritten = 0;
int byteCount = 0;
byte[] bytes = new byte[1024];
while ((byteCount = inputStream.read(bytes)) != -1)
{
outputStream.write(bytes, bytesWritten, byteCount);
bytesWritten += byteCount;
}
inputStream.close();
outputStream.close();
追问
我的意思是流里面既有文件内容,又有数据内容噢。
比如对方传送的报文是{a:"1",b"2"},对方传送的文件是a.txt,而流里面即会有报文内容又有文件内容,如何区分呢
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询