根据传入的文件名称动态从moglifs图片服务器拿到pdf文档并在线浏览

2023-06-12,,

1.通过百度编辑器上传pdf文档等附件时,在上传方法中将返回的url进行设定,以达到后期点击后可进行浏览的效果:

public static final State save(HttpServletRequest request,
Map<String, Object> conf) {
FileItemStream fileStream = null;
boolean isAjaxUpload = request.getHeader( "X_Requested_With" ) != null; if (!ServletFileUpload.isMultipartContent(request)) {
return new BaseState(false, AppInfo.NOT_MULTIPART_CONTENT);
} ServletFileUpload upload = new ServletFileUpload(
new DiskFileItemFactory()); if ( isAjaxUpload ) {
upload.setHeaderEncoding( "UTF-8" );
} try {
FileItemIterator iterator = upload.getItemIterator(request); while (iterator.hasNext()) {
fileStream = iterator.next(); if (!fileStream.isFormField())
break;
fileStream = null;
} if (fileStream == null) {
return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
} String savePath = (String) conf.get("savePath");
String originFileName = fileStream.getName();
String suffix = FileType.getSuffixByFilename(originFileName);
//将名字重复名,以免重复导致图片被覆盖并且防由于特殊字符导致无法上传
originFileName = DateFormatHelper.getNowTimeStr("yyyyMMddHHmmssSSS")+CodeManager.getRandomNum(10);//originFileName.substring(0,originFileName.length() - suffix.length());
savePath = savePath + suffix; long maxSize = ((Long) conf.get("maxSize")).longValue(); if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
} savePath = PathFormat.parse(savePath, originFileName); String physicalPath = (String) conf.get("rootPath") + savePath; InputStream is = fileStream.openStream(); FileModel filemodel=uploadFiles(is, originFileName + suffix);
System.out.println(filemodel.toString()); State storageState = new BaseState(true);
String temp=filemodel.getRemotePaths();
String temp2=temp.substring(getCharacterPosition(temp), temp.length());
System.out.println(temp2);
storageState.putInfo( "title", filemodel.getFileName()); if (storageState.isSuccess()) {
//storageState.putInfo("url", temp2);//PathFormat.format(savePath)
storageState.putInfo("url", getDownloadPath(suffix, originFileName + suffix, temp2));
storageState.putInfo("type", suffix);
storageState.putInfo("original", originFileName + suffix);
} return storageState;
} catch (FileUploadException e) {
log.error("FileUploadException",e);
return new BaseState(false, AppInfo.PARSE_REQUEST_ERROR);
} catch (IOException e) {
log.error("IOException",e);
}
return new BaseState(false, AppInfo.IO_ERROR);
}

设定返回url方法:

/**
* 返回可在线浏览pdf的url
* @param suffix
* @param fileName
* @param imgUrl
* @return
*/
private static String getDownloadPath(String suffix,String fileName,String imgUrl){
String[] downFileSuffix = new String[]{".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml",".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso"};
List<String> list = Arrays.asList(downFileSuffix);
String returnUrl = imgUrl;
if(list.contains(suffix)){
returnUrl = "/browsePdf?enclosure="+fileName;
}
return returnUrl;
}

2.下边为在线浏览的控制层方法:

/**
* 在线浏览pdf
* @param request
* @param response
* hdf
* @throws Exception
*/
@RequestMapping(value = "/browsePdf", method = RequestMethod.GET)
public void browsePdf(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String enclosure = request.getParameter("enclosure");
// 在线浏览pdf
fileUpload.browsePdf(request, response, enclosure);
}

通过文件名获取流,将流以pdf的格式输出,注意:1.格式必须设置为pdf,2,需要将浏览的类型设置为:inline

/**
* 通过web应用来l浏览pdf文档
* @param request HttpServletRequest对象
* @param response HttpServletResponse对象
* @param fileName 真实的文件名字,也就是记录在File模块里面的名字
* Add by hdf when 2017.10.10
*/
public void browsePdf(HttpServletRequest request,
HttpServletResponse response, String fileName){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
//设置字符集
//response.setContentType("charset=UTF-8");
response.setContentType("application/pdf");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8"); //根据文件名字在file记录里面获取相应的记录
//FileModel fm = fileup.getOneFileModel(fileName);
FileModel fm = fileService.getOneFileModel(fileName);
String mogilefsPath = fm.getRemotePaths();
//设置文件下载中,对话框显示的文件名字,也就是真实名字
response.setHeader("Content-disposition", "inline; filename="
+ new String(fm.getFileName().getBytes("gb2312"), "ISO8859-1")); //通过URL从Mogilefs去获取文件内容
URL url = new URL(mogilefsPath); bis = new BufferedInputStream(url.openStream());
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[1024];
int bytesRead = 0;
//判断是否需要编码转换
int lastPointSite = fm.getFileName().lastIndexOf(".");
String fileType = fm.getFileName().substring(lastPointSite);
boolean needTrans = this.needTransCharacterEncoding.contains(fileType);
//流式获取内容并流式输出
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
if(needTrans){
String tempS = new String(buff,"utf-8");
bos.write(tempS.getBytes());
buff = new byte[1024];
}else{
bos.write(buff, 0, bytesRead);
}
}
if(bos !=null){
bos.close();
}
}catch(Exception err){
log.error(err);
// err.printStackTrace();
throw new LogException("file.downLoad", "downloadFile error,fileName="+fileName, err);
}finally{
try{
if(bis !=null){
bis.close();
}
}catch(Exception err){
log.error(err);
// err.printStackTrace();
}
try{
if(bos !=null){
bos.close();
}
}catch(Exception err){
log.error(err);
// err.printStackTrace();
}
}
}

根据传入文件名称动态从moglifs图片服务器拿到pdf文档并在线浏览的相关教程结束。

《根据传入的文件名称动态从moglifs图片服务器拿到pdf文档并在线浏览.doc》

下载本文的Word格式文档,以方便收藏与打印。