首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

servlet使用commons-fileupload上传文件示范

2014-01-05 
servlet使用commons-fileupload上传文件示例private static final Log log LogFactory.getLog(Transport

servlet使用commons-fileupload上传文件示例

private static final Log log = LogFactory.getLog(TransportDataServlet.class);@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);diskFileItemFactory.setRepository(new File(getServletContext().getRealPath("/")));//diskFileItemFactory.setSizeThreshold(1024 *1024);diskFileItemFactory.setSizeThreshold(2<<19);try {List<FileItem> list = servletFileUpload.parseRequest(req);for (FileItem fileItem : list) {if (true == fileItem.isFormField()) {String name = fileItem.getFieldName();String value = fileItem.getString();log.info(name);log.info(value);} else {File xml = new File(getServletContext().getRealPath("/"),System.currentTimeMillis()+ FilenameUtils.getName(fileItem.getName()));BufferedInputStream bufferedInputStream = new BufferedInputStream(fileItem.getInputStream());BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(xml));byte[] buff = new byte[1024];for (int len = 0; (len = bufferedInputStream.read(buff)) > 0;) {bufferedOutputStream.write(buff, 0, len);}bufferedOutputStream.close();bufferedInputStream.close();}}} catch (FileUploadException e) {log.error(e.getMessage(), e);}


获取ServletContext方法
批量修改文件的编码

热点排行