BufferedInputStream bis = null;
ByteArrayOutputStream bos = null;
FileOutputStream fos = null;
try
{
bis = new BufferedInputStream(requset.getInputStream());
bos = new ByteArrayOutputStream();
int len = 0;
// 读取
byte[] buffer = new byte[409600];
while ((len = bis.read(buffer)) > -1)
{
bos.write(buffer, 0, len);
}
byte[] allData = bos.toByteArray();
// 写入
fos = new FileOutputStream(new File("G://msc.jpg"));
fos.write(allData);
fos.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
fos.close();
bos.close();
bis.close();
}
ByteArrayOutputStream bos = null;
FileOutputStream fos = null;
try
{
bis = new BufferedInputStream(requset.getInputStream());
bos = new ByteArrayOutputStream();
int len = 0;
// 读取
byte[] buffer = new byte[409600];
while ((len = bis.read(buffer)) > -1)
{
bos.write(buffer, 0, len);
}
byte[] allData = bos.toByteArray();
// 写入
fos = new FileOutputStream(new File("G://msc.jpg"));
fos.write(allData);
fos.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
fos.close();
bos.close();
bis.close();
}
本文提供了一个使用Java进行文件上传的示例代码。通过BufferedInputStream从请求中读取数据,然后利用ByteArrayOutputStream缓存数据,最后将数据写入到指定路径的文件中。此示例适用于理解Java文件上传的基本流程。
2万+

被折叠的 条评论
为什么被折叠?



