java使用common-fileupload实现文件上传

这篇文章主要为大家详细介绍了java使用common-fileupload实现文件上传的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

文件上传是网站非常常用的功能,直接使用Servlet获取上传文件还得解析请求参数,比较麻烦,所以一般选择采用apache的开源工具,common-fileupload.这个jar包可以再apache官网上面找到,也可以在struts的lib文件夹下面找到,struts上传功能就是基于这个实现的。

common-fileupload是依赖于common-io这个包的,所以还需要下载这个包。然后导入到你的项目路径下面。

使用代码如下

package oop.hg.ytu.servlet; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.List; import javax.servlet.servletexception; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import oop.hu.ytu.dao.UploadDomain; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.diskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class Upload extends HttpServlet { /** * 处理用户上传请求 */ private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws servletexception, IOException { // String describe = request.getParameter("describe"); diskFileItemFactory factory = new diskFileItemFactory(); @SuppressWarnings("deprecation") String path = request.getRealPath("/upload");//设置磁盘缓冲路径 factory.setRepository(new File(path)); factory.setSizeThreshold(1024*1024);//设置创建缓冲大小 ServletFileUpload upload = new ServletFileUpload(factory); upload.setSizeMax(-1);//设置上传文件限制大小,-1无上限 try { @SuppressWarnings("unchecked") List list = upload.parseRequest(request); String va = null; for(FileItem item : list){ // String name = item.getFieldName(); if(item.isFormField()){//判断是否是文件流 va = item.getString("UTF-8"); // System.out.println(name+"="+va); /// request.setAttribute(name, value); }else{ String value = item.getName();//会将完整路径名传过来 int start = value.lastIndexOf("\"); String fileName = value.substring(start+1); // request.setAttribute(name, fileName); InputStream in = item.getInputStream(); UploadDomain dao = new UploadDomain(); //item.write(new File(realPath,fileName)); int index = fileName.lastIndexOf("."); String realFileName = fileName.substring(0,index); String type = fileName.substring(index+1); dao.insert(in, realFileName,type,va);//放入到数据库中 } } } catch (Exception e) { e.printstacktrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws servletexception, IOException { doGet(request, response); } }

 这里分别判断是否是上传的流或者表单里面的参数,比如文本框提交信息,然后将他们插入到数据库中。数据库插入

代码如下

package oop.hu.ytu.dao; import java.io.InputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import oop.hg.ytu.utils.JdbcUtils; /** * 提供文件上传支持 * @author Administrator * */ public class UploadDomain { /** * 将上传文件流放入到数据库中 */ public void insert(InputStream in, String fileName, String type,String describe) throws Exception{//向数据库写入图片 Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; System.out.println(describe); try { // 2.建立连接 conn = JdbcUtils.getConnection(); // 3.创建语句 String sql = "insert into fileupload(file,filename,type,des) values (?,?,?,?)"; ps = conn.prepareStatement(sql); ps.setBlob(1, in); ps.setString(2, fileName); ps.setString(3, type); ps.setString(4, describe); // 4.执行语句 ps.executeUpdate(); in.close(); } finally { JdbcUtils.free(rs, ps, conn); } } }

可能会遇到数据库认问价大小限制,需要在MysqL安装目录下面的my.ini下面更改如下配置,

[MysqLd] 

max_allowed_packet=64M 

这样就可以了。当然,注意编码格式。上传文件搞定。还有就是我的一个列名设置为describe,结果和MysqL保留字冲

突,出现无法插入信息现象,以后一定要注意。

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...