/** 下载T&C文件 */ public ActionForward downloadTAndC(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { if (!isLogined(request)) { return mapping.findForward("login"); } String path = getServlet().getServletContext().getRealPath("/groupbuying/pdf/"); String filename = request.getParameter("filename"); if (filename == null || "".equals(filename.trim())) { filename = "tAndC.pdf"; } File file = new File(path + File.separatorChar + filename); log.info("[GroupBuyingMaintainAction]downloadTAndC--filePath=" + path + File.separatorChar + filename); if (file.exists()) { InputStream is = null; BufferedInputStream bis = null; OutputStream os = null; BufferedOutputStream bos = null; try { is = new FileInputStream(file); bis = new BufferedInputStream(is); os = response.getOutputStream(); bos = new BufferedOutputStream(os); response.setContentType("application/x-download"); response.setHeader("Content-Disposition", "attachment;filename=tAndC.pdf"); int read = 0; byte[] buffer = new byte[1024]; while ((read = bis.read(buffer, 0, 1024)) != -1) { bos.write(buffer, 0, read); } bos.flush(); } catch (FileNotFoundException e) { log.error("[downloadTAndC]", e); throw e; } catch (IOException e) { log.error("[downloadTAndC]", e); throw e; } finally { if (bos != null) bos.close(); if (os != null) os.close(); if (bis != null) bis.close(); if (is != null) is.close(); } } return null; }
/** 上传offer图片 */public ActionForward uploadImage(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {boolean isMultipart = ServletFileUpload.isMultipartContent(request);System.out.println("[GroupBuyingMaintainAction]uploadImage--isMultipart=" + isMultipart);if (isMultipart) {FileItemFactory factory = new DiskFileItemFactory();ServletFileUpload upload = new ServletFileUpload(factory);Iterator<?> items;try {// 解析表单中提交的所有文件内容items = upload.parseRequest(request).iterator();while (items.hasNext()) {FileItem item = (FileItem) items.next();System.out.println("[GroupBuyingMaintainAction]uploadImage--item.isFormField=" + item.isFormField());if (!item.isFormField()) {// 文件名String filename = item.getName();log.info("[GroupBuyingMaintainAction]uploadImage--filename=" + filename);// 过滤类型if (!item.getContentType().startsWith("image/")) {throw new Exception("Upload Type invalid");}// 上传文件以后的存储路径String path = getServlet().getServletContext().getRealPath("/groupbuying/images/");File f = new File(path);if (!f.exists()) {if (!f.mkdir()) {break;}}// 重命名文件名filename = "img_" + new Date().getTime() + filename.substring(filename.indexOf('.'));String filePath = path + File.separatorChar + filename;// 上传文件log.info("[GroupBuyingMaintainAction]uploadImage--Start Upload Image...filePath=" + filePath);File uploaderFile = new File(filePath);System.out.println("[GroupBuyingMaintainAction]uploadImage--file exist=" + uploaderFile.exists());item.write(uploaderFile);log.info("[GroupBuyingMaintainAction]uploadImage--End Upload Image");// 写进DBofferService.insertFileToDB(filePath);response.setStatus(HttpServletResponse.SC_OK);response.getWriter().append(filename);}}} catch (Exception e) {log.error("upload", e);response.setStatus(HttpServletResponse.SC_OK);response.getWriter().append("");}}return null;}
1、上传文件2、下载文件
java上传与下载文件
最新推荐文章于 2022-10-12 22:38:45 发布