SSM上传PDF并预览

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LLnobug/article/details/80487185

两种方式:一种传到项目下,直接预览;另一种传到专门的存储位置,通过设置虚拟路径预览。

废话不多说,正文开始:

1.上传到项目下并预览。

控制器:

        @RequestMapping(value = "/uploadPDF", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> UploadPDF(@RequestParam("file") CommonsMultipartFile file,
			HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String sno = "123456"; // (String)request.getSession().getAttribute("username");


		// 获得原始文件名
		String fileName = file.getOriginalFilename();
		System.out.println("原始文件名:" + fileName);


		// 新文件名
		String newFileName = sno + "docPDF.pdf";


		// 获得项目的路径
		ServletContext sc = request.getSession().getServletContext();
		// 上传位置
		String path = sc.getRealPath("/uploadPDF") + "/"; // 设定文件保存的目录


		File f = new File(path);
		if (!f.exists())
			f.mkdirs();
		if (!file.isEmpty()) {
			try {
				FileOutputStream fos = new FileOutputStream(path + newFileName);
				InputStream in = file.getInputStream();
				int b = 0;
				while ((b = in.read()) != -1) {
					fos.write(b);
				}
				fos.close();
				in.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}


		System.out.println("上传pdf到:" + path + newFileName);


		map = JsonModelUtil.JsonModel("1", "文件提交成功", 1, null);
		return map;
	}

上传jsp:

<form action="path/uploadPDF1" method="post" enctype="multipart/form-data">
      协议书:   
      <input type="file" class="form-horizontal" name="file" /> <input type="submit" value="上 传" />
</form>

预览jsp:

<iframe  width=100% height=800  scrolling='no' frameborder='0' src='/GraduationProject/uploadPDF/${param.sno }docPDF.pdf' ></iframe>

如果是传到项目下边可以直接预览,不用做其他设置,优点是方便简单,缺点是如果需要上传的文件过多,会造成项目体积过大,对文件过多的情况不适用。所以可以考虑放在专门的存储位置,然后通过虚拟路径预览。

2.上传到存储位置并预览。

控制器:

        @RequestMapping(value = "/uploadPDF1", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> UploadPDF1(HttpServletRequest request, HttpServletResponse response) throws Exception {
		CommonsMultipartResolver cmr = new CommonsMultipartResolver(request.getServletContext());
		Map<String, Object> map = new HashMap<String, Object>();
		String sno = "201511105079"; // (String)request.getSession().getAttribute("username");
		
		if (cmr.isMultipart(request)) {
			MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) (request);
			Iterator<String> files = mRequest.getFileNames();
			while (files.hasNext()) {
				MultipartFile mFile = mRequest.getFile(files.next());
				if (mFile != null) {
					String fileName =  sno + "docPDF.pdf";
					String path = "d:/test/" + fileName;

					File localFile = new File(path);
					mFile.transferTo(localFile);
					System.out.println("上传pdf到:" + path);
				}
			}
		}
		map = JsonModelUtil.JsonModel("1", "文件提交成功", 1, null);
		return map;
	}

上传jsp同上一种方式。

预览jsp:

<iframe  width=100% height=800  scrolling='no' frameborder='0' src='/upload/${param.sno }docPDF.pdf' ></iframe>

那么问题来了,这样写不能预览,会报404,这就需要通过设置虚拟路径来完成了。

Tomcat下设置虚拟路径:

找到Tomcat下的conf文件夹,打开server.xml文件,在<Host></Host>之间加上下面这短代码。

<!--增加虚拟路径,用于预览,path="/虚拟名" docBase="虚拟路径"-->
<Context path="/upload" docBase="d:/test/" reloadable="true"></Context>

注意:一定要加在<Host></Host>之间。

如果需要在eclipse测试时可用,则需要在eclipse里进行配置,如下:


设置方式和上面tomcat下设置一样。

然后你就可以直接实现预览的,现在大多数浏览器是支持这种预览方式的。

猜你喜欢

转载自blog.csdn.net/LLnobug/article/details/80487185
今日推荐