java上传与下载文件

/** 下载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、下载文件




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值