private void Upload2(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Part part = request.getPart("upload");
String name = part.getName();
String header = part.getHeader("Content-Disposition");
int i = header.lastIndexOf("filename=\"");
//获取上传的文件名
String fileName = header.substring(i+10, header.length()-1);
System.out.println("文件名大小是:"+part.getSize());
System.out.println("文件名是:"+fileName+"=====文件上传项的value属性值是:"+name);
System.out.println("文件头信息Content-Disposition是:"+header);
//获取输入流
InputStream is = part.getInputStream();
String realPath = request.getServletContext().getRealPath("/upload");
//创建输出流
FileOutputStream os = new FileOutputStream(realPath+"/"+fileName);
//读写操作
byte[] byts = new byte[1024];
int len = 0;
while ( (len = is.read(byts)) != -1 ) {
os.write(byts, 0, len);
}
//IOUtils.copy(is, os);
//关闭流
os.close();
is.close();
}
95.8 KB, 下载次数: 44
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |