谷歌云 wordpress 建站,凯里做网站的公司,湛江网站制作费用,wordpress建立数据库错误commons-fileupload是Apache提供的一个实现文件上传下载的简单#xff0c;有效途径,需要commons-io包的支持#xff0c;本文是一个简单的示例
上传页面,注意设置响应头
bodycenterh1文件上传页面/h1hrform action${page…commons-fileupload是Apache提供的一个实现文件上传下载的简单有效途径,需要commons-io包的支持本文是一个简单的示例
上传页面,注意设置响应头
bodycenterh1文件上传页面/h1hrform action${pageContext.request.contextPath }/servlet/UploadServlet methodpost enctypemultipart/form-data选择文件input typefile namefile1/br描述信息:textarea rows5 cols45 namediscription/textareabrinput typesubmit value上传//form/center/body
上传的servlet
//上传文件String uploadthis.getServletContext().getRealPath(WEB-INF/upload);String tempthis.getServletContext().getRealPath(WEB-INF/temp);Map pmapnew HashMap();//get client IP addresspmap.put(ip, request.getRemoteAddr());DiskFileItemFactory factorynew DiskFileItemFactory();//设定内存缓冲区大小 Set the memory buffer sizefactory.setSizeThreshold(1024*100);//指定临时文件目录 Specifies the directory for temporary filesfactory.setRepository(new File(temp));ServletFileUpload fileUploadnew ServletFileUpload(factory);fileUpload.setHeaderEncoding(utf-8);fileUpload.setFileSizeMax(1024*1024*100);fileUpload.setSizeMax(1024*1024*200);//set form style enctypemultipart/form-dataif(!fileUpload.isMultipartContent(request)){throw new RuntimeException(请使用正确的表单进行上传);}//解析requesttry {ListFileItem list fileUpload.parseRequest(request);//遍历listfor(FileItem item:list){if(item.isFormField()){String nameitem.getFieldName();String valueitem.getString(utf-8);pmap.put(name, value);}else{String realnameitem.getName();String arry[]realname.split(\\\\);realnamearry[arry.length-1];System.out.println(realname);String uuidNameUUID.randomUUID().toString()_realname;pmap.put(realname, realname);pmap.put(uuidname, uuidName);InputStream initem.getInputStream();String hashInteger.toHexString(uuidName.hashCode());String savepath/WEB-INF/upload;for(char c:hash.toCharArray()){upload/c;savepath/c;}new File(upload).mkdirs();pmap.put(savepath, savepath);OutputStream outnew FileOutputStream(new File(upload,uuidName));IOUtils.In2Out(in, out);IOUtils.close(in, out);item.delete();}}} catch (Exception e) {// TODO 自动生成的 catch 块e.printStackTrace();}//向数据库中插入数据Resourse rnew Resourse(); try {BeanUtils.populate(r, pmap);String sqlinsert into netdisk values(null,?,?,?,?,null,?);QueryRunner runnernew QueryRunner(DaoUtils.getSource());runner.update(sql,r.getUuidname(),r.getRealname(),r.getSavepath(),r.getIp(),r.getDescription());} catch (Exception e) {// TODO 自动生成的 catch 块e.printStackTrace();} //重定向回主页response.sendRedirect(request.getContextPath()/index.jsp);
为防止重名所以使用了UUIDNAME,把文件上传到web-inf/upload文件夹下并且将路径与文件名保存到数据库中上传功能完成
下载实现
下载页面
bodycenterh1下载列表/h1hrc:forEach items${requestScope.list } varrh2文件名:${r.realname }br/h2上传时间:${r.uploadtime }br上传者IP${r.ip }br描述信息${r.description }bra href${pageContext.request.contextPath }/servlet/DownServlet?id${r.id}下载/abrhr/c:forEach/center/body
下载实现 response.setContentType(text/html;charsetutf-8);//获取IDString idrequest.getParameter(id);//根据ID查找资源String sqlselect * from netdisk where id?;Resourse rnull;QueryRunner runnernew QueryRunner(DaoUtils.getSource());try {r runner.query(sql, new BeanHandlerResourse(Resourse.class), id);} catch (SQLException e) {// TODO 自动生成的 catch 块e.printStackTrace();}if(rnull){response.getWriter().write(找不到该资源!!!!);return;}else{//指定响应头response.setHeader(Content-Disposition, attachment;filenameURLEncoder.encode(r.getRealname(),UTF-8));response.setContentType(this.getServletContext().getMimeType(r.getRealname()));String filePaththis.getServletContext().getRealPath(r.getSavepath()/r.getUuidname());InputStream innew FileInputStream(filePath);OutputStream outresponse.getOutputStream();IOUtils.In2Out(in, out);IOUtils.close(in, null);}
上传下载完成注意下载时一定要指定两个响应头
IO工具类
public class IOUtils {private IOUtils() {}public static void In2Out(InputStream in,OutputStream out) throws IOException{byte [] bs new byte[1024];int i 0;while((iin.read(bs))!-1){out.write(bs,0,i);}}public static void close(InputStream in,OutputStream out){if(in!null){try {in.close();} catch (IOException e) {e.printStackTrace();}finally{in null;}}if(out!null){try {out.close();} catch (IOException e) {e.printStackTrace();}finally{out null;}}}
}
完成