文件上传以及导入二进制到数据库程序心得
2013-12-26 09:32
696 查看
2013-12-26 08:49:22 frank
写文件上传以及导入到数据库程序心得
----------------------------------
项目采用的框架是:
Spring MVC
上传功能的实现:
step1,在配置文件中加上下面的配置
step2,在请求处理方法里通过
step3,通过流将文件重新写出来
解析文件时遇到的问题 :
写文件上传以及导入到数据库程序心得
----------------------------------
项目采用的框架是:
Spring MVC
上传功能的实现:
step1,在配置文件中加上下面的配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name='maxUploadSize'> <!--限制最大文件是10M--> <value>100010485761</value> </property> </bean>
step2,在请求处理方法里通过
// 将request转换成MultipartHttpServletRequest MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile mfile = multipartRequest.getFile("file"); // 获得文件 InputStream input = mfile.getInputStream(); // 获得输入流
step3,通过流将文件重新写出来
// 存放文件的目录 File dir = new File(proPath + "/WEB-INF/madecarddata"); if (dir.exists()) { dir.mkdir(); } // 生成文件的路径 File file = new File(dir + "/" + filename); System.out.println(file.getName()); FileOutputStream fos; try { fos = new FileOutputStream(file); byte[] bbs = new byte[1024]; int len = -1; while ((len = in.read(bbs)) != -1) { fos.write(bbs, 0, len); } in.close(); fos.close(); return true; } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("生成文件失败"); return false; } catch (IOException e) { e.printStackTrace(); System.out.println("生成文件失败"); return false; }
解析文件时遇到的问题 :
//1.乱码问题 String encoding = "GBK"; InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式 //或者 str = bufferedReader.readLine(); st = new String(str.getBytes(),"GBK");
//2.上传二进制文件到数据库 //首先要关闭自动提交 session.connection().setAutoCommit(false); //在return前手动提交 session.connection().commit();
相关文章推荐
- 上传文件实现数据采集以及批量导入数据库实例
- 关于上传文件,以及Excel数据导入数据库
- 文件信息批量导入数据库(Struts2实现文件上传,POI实现Excel文件读取并写入数据库) .
- 文件的上传(本文是把文件传到磁盘)(上传到数据库请访问:EXCEL的数据导入ORACLE中ja...
- MongoDB数据库的文件备份恢复以及文件导入导出
- 详细贯穿Golang和Python的第一个程序,以及对应的二进制文件编译方法
- 采用struts2+hibernate实现保存文件到Oracle数据库&&并附下载代码【导入即可用】;数据库中保存blob类型的二进制文件
- struts2.X心得8--struts与servlet的松耦合scope之间的访问以及文件上传
- javaweb 文件blob上传到数据库以及反显
- MongoDB数据库的文件备份恢复以及文件导入导出
- 将上传文件以二进制形式存入数据库中,并下载数据库中的二进制数据生成对应的文件
- smartupload实现 jsp页面上传文件 将文件以二进制形式保存在数据库
- 一键式安装.Net FrameWork,MSDE,Web安装程序,Windows安装程序,还原数据库文件,注册表导入
- struts2.X心得8--struts与servlet的松耦合scope之间的访问以及文件上传
- struts2.X心得9--struts2自定义拦截器以及文件上传等几个拦截器使用案例
- springMVC将txt文件或Excel文件中的内容上传并导入数据库
- 使用ADO GetChunk/AppendChunk 数据库存取二进制文件(如程序,图象等)
- MongoDB数据库的文件备份恢复以及文件导入导出
- 文件上传 下载 一般处理程序和webForm联系以及asp.net内置对象
- FMDB使用的一点心得:数据库创建、制表、查询等以及image转换成二进制nsdata保存到数据库中