`

struts1 多文件上传

    博客分类:
  • J2EE
OS 
阅读更多
public ActionForward upload(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MuliUploadForm m = (MuliUploadForm) form;
		Hashtable map = m.getMultipartRequestHandler().getFileElements();
		Enumeration enums = map.keys();

		if (map != null && map.size() > 0) {

			while (enums.hasMoreElements()) {
				String key = (String) enums.nextElement();
				FormFile formFile = (FormFile) map.get(key);
				String path = request.getSession().getServletContext()
						.getRealPath("/upload")
						+ "/" + formFile.getFileName();
				uploads(formFile, path);
			}

		}

		return null;
	}

	// 文件上传
	private void uploads(FormFile formFile, String path) throws Exception {
		InputStream is = formFile.getInputStream();
		OutputStream os = new FileOutputStream(new File(path));
		byte[] b = new byte[1024];
		int len = 0;

		while ((len = is.read(b)) != -1) {
			os.write(b, 0, len);
		}

		os.flush();
		os.close();
		is.close();
		System.out.println("上传成功");
	}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics