portal JSR168 上传下载
在portlet jsR 168中实现上传下载的问题
/******portlet上传 public class Fileupload extends GenericPortlet {
public static final String JSP_FOLDER = "/download/jsp/html/"; // JSP folder name
public static final String VIEW_JSP = "FileuploadView"; // JSP file name to be rendered on the view mode
public static final String SESSION_BEAN = "FileuploadSessionBean"; // Bean name for the portlet session
public static final String FORM_SUBMIT = "FileuploadFormSubmit"; // Action name for submit form public static final String FORM_TEXT = "FileuploadFormText"; // Parameter name for the text input private String tmp_pic_path =""; private String filePath =""; private String Pic_Path=""; /**
- @see javax.portlet.Portlet#init(javax.portlet.PortletConfig) */ public void init(PortletConfig config) throws PortletException{ super.init(config); tmp_pic_path=config.getPortletContext().getRealPath("/WEB-INF/classes"); //System.out.print("全路径为:"+tmp_pic_path); }
/**
- Serve up the view mode.
- @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse) */ public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException { // Set the MIME type for the render response response.setContentType(request.getResponseContentType());
// Check if portlet session exists
FileuploadSessionBean sessionBean = getSessionBean(request);
if( sessionBean==null ) {
response.getWriter().println("NO PORTLET SESSION YET");
return;
}
String doc_paths = tmp_pic_path.split("\\");
String tmp_path = "";
for(int i=0;i<doc_paths.length;i++){
if(i<doc_paths.length-2){
tmp_path +=doc_paths*+"\";
}
}
filePath = tmp_path;
// Invoke the JSP to render PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(sessionBean.getViewJsp()); rd.include(request,response); }
/**
- Process an action request.
- @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
/
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {
FileuploadSessionBean sessionBean = getSessionBean(request);
if( sessionBean==null ) {
return; } ServiceLocator sl=null; SingleTableSessionHome SingleTablehome = null; SingleTableSession singleTable =null; try {
sl = ServiceLocator.getInstance(); SingleTablehome = (SingleTableSessionHome)sl.getHome("ejb/SingleTableSessionBean",SingleTableSessionHome.class); singleTable=SingleTablehome.create(); } catch (Exception e) {
e.printStackTrace(); } String fileType =""; try{ // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); PortletFileUpload upload = new PortletFileUpload(factory);
//设置factory的大小; factory.setSizeThreshold(201024); factory.setRepository(new File(filePath));
List fileItems = upload.parseRequest(request); //解析请求,返回一个集合. upload.setFileSizeMax(2010241024); Iterator i = fileItems.iterator(); while(i.hasNext()) { FileItem fi = (FileItem)i.next();
if(fi.isFormField()) //这是用来确定是否为文件属性, {
String fieldName = fi.getFieldName(); //这里取得表单名 if(fieldName.equalsIgnoreCase("fileType")){ fileType = fi.getString();//获得表单值 System.out.print("表单的值为:"+fileType); }
} else //这里开始处理文件 { String fileName = fi.getName(); // 返回文件名包括客户机路径 long size = fi.getSize();// 获取上传的文件大小(字节为单位)
if ((fileName == null || fileName.equals("")) && size == 0) continue;// 跳到while检查条件 int end = fileName.length(); // 返回在此字符串中最右边出现的指定子字符串的索引。 int begin = fileName.lastIndexOf("\"); String name = fileName.substring(begin + 1, end);
filePath +=fileType; System.out.print("全路径为:"+filePath); File savedFile = new File(filePath,name);
fi.write(savedFile);
} } }catch(Exception e){
e.printStackTrace(); } sessionBean.setSussesStr("是"); sessionBean.setViewJsp("uploadjsp.jsp");
}
/**
- Get SessionBean.
- @param request PortletRequest
- @return BasicPortletSessionBean */ private static FileuploadSessionBean getSessionBean(PortletRequest request) { PortletSession session = request.getPortletSession(); if( session == null ) return null; FileuploadSessionBean sessionBean = (FileuploadSessionBean)session.getAttribute(SESSION_BEAN); if( sessionBean == null ) { sessionBean = new FileuploadSessionBean(); session.setAttribute(SESSION_BEAN,sessionBean); } return sessionBean; } }
//就是在doView把文件要上传的路径得到.当然你可以自己决定上传到固定的硬盘下.在这里我是上传到我的工程项目下的一个文件夹下.所以要在doview中的得到要上传的路径. //***********上传的jsp
public void init(PortletConfig config) throws PortletException{ super.init(config); classes_path = config.getPortletContext().getRealPath("/WEB-INF/classes"); }
/**
- Serve up the view mode.
- @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse) */ public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException { // Set the MIME type for the render response response.setContentType(request.getResponseContentType());
// Check if portlet session exists FiledownloadSessionBean sessionBean = getSessionBean(request); if( sessionBean==null ) { response.getWriter().println("NO PORTLET SESSION YET"); return; } String doc_paths = classes_path.split("\\"); String tmp_path = ""; for(int i=0;i<doc_paths.length;i++){ if(i<doc_paths.length-2){ tmp_path +=doc_paths*+"\"; }
}
filePath = tmp_path;
// 取出是系统帮助文档
String pathsystem = tmp_path+"systemhelp";
//在这里是获得 pathsystem 文件夹的文件.
File sfile=new File(pathsystem);
//在这里是获得 pathsystem 文件夹下的所有文件.并且放在数组中
String flist=sfile.list();
//利用sessionBean传到jsp页面上.
sessionBean.setSystemFiles(flist);
// Invoke the JSP to render PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(sessionBean.getViewJSP()); rd.include(request,response); } /////////// 在这里同样是在init() 中获得文件的路径.在doview()中获得要下载的文件夹的路径, /////////////////////download.jsp***********
<%@ page import="javax.portlet.,download." %> <%@ page language="java" contentType="text/html;charset=GB18030" pageEncoding="GB18030" session="false"%> <%@taglib uri="http://java.sun.com/portlet" prefix="portlet" %> portlet:defineObjects/
<% FiledownloadSessionBean sessionBean = (FiledownloadSessionBean)renderRequest.getPortletSession().getAttribute(Filedownload.SESSION_BEAN); String systemfiles = sessionBean.getSystemFiles(); %>
} %> ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// 这就基本实现了下载.如果是图片就直接用ie打开.如果是word文档就弹出下载的对话框.
转自http://antheahuimin.blog.163.com/blog/static/180691320077301154094/