在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 <code>view</code> 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("<b>NO PORTLET SESSION YET</b>");
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[i]+"\\";
}
}
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(20*1024);
factory.setRepository(new File(filePath));
List fileItems = upload.parseRequest(request); //解析请求,返回一个集合.
upload.setFileSizeMax(20*1024*1024);
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***************************************
<FORM method="POST" action="<portlet:actionURL/>" name="uploadform" enctype="multipart/form-data">
<TABLE border="1">
<TR>
<TD width="285">文件类型:</TD>
<TD width="681">
<SELECT name="fileType">
<OPTION value="systemhelp">系统帮助文档</OPTION>
<OPTION value="tables">常用表格</OPTION>
<OPTION value="temp">其他</OPTION>
</SELECT>
</TD>
</TR>
<TR>
<TD width="285">文件上传</TD>
<TD width="681"><input type="file" name="filePath"></TD>
</TR>
</TABLE>
<p><INPUT name="shangchuan" type="submit" value="上传"/></p>
</FORM>
/////////////////////////////////////////////////////////////////////////////////////////////////////
有了上传下面是下载
大家都知道,在portal中不好来设置content type,无法获得输出流.最好的方式就是在doview()中通过iframe在servlet中实现.
在这里我是用文件的形势实现下载的.
////////////////////////////////////////////////////////////////////////////////////////////////////
//*********************************************portlet 下载****************************************************************
public void init(PortletConfig config) throws PortletException{
super.init(config);
classes_path = config.getPortletContext().getRealPath("/WEB-INF/classes");
}
/**
* Serve up the <code>view</code> 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("<b>NO PORTLET SESSION YET</b>");
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[i]+"\\";
}
}
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();
%>
<p><font color="red">系统帮助文档下载</font></p>
<%
if(systemfiles!=null){
for(int i=0; i<systemfiles.length;i++){
%><p> <a href ="<%=renderResponse.encodeURL(renderRequest.getContextPath()) %>/systemhelp/<%=systemfiles[i] %>"><%=systemfiles[i] %></a></p><%
}
}
%>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
这就基本实现了下载.如果是图片就直接用ie打开.如果是word文档就弹出下载的对话框.
转自http://antheahuimin.blog.163.com/blog/static/180691320077301154094/
/**************************************************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 <code>view</code> 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("<b>NO PORTLET SESSION YET</b>");
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[i]+"\\";
}
}
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(20*1024);
factory.setRepository(new File(filePath));
List fileItems = upload.parseRequest(request); //解析请求,返回一个集合.
upload.setFileSizeMax(20*1024*1024);
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***************************************
<FORM method="POST" action="<portlet:actionURL/>" name="uploadform" enctype="multipart/form-data">
<TABLE border="1">
<TR>
<TD width="285">文件类型:</TD>
<TD width="681">
<SELECT name="fileType">
<OPTION value="systemhelp">系统帮助文档</OPTION>
<OPTION value="tables">常用表格</OPTION>
<OPTION value="temp">其他</OPTION>
</SELECT>
</TD>
</TR>
<TR>
<TD width="285">文件上传</TD>
<TD width="681"><input type="file" name="filePath"></TD>
</TR>
</TABLE>
<p><INPUT name="shangchuan" type="submit" value="上传"/></p>
</FORM>
/////////////////////////////////////////////////////////////////////////////////////////////////////
有了上传下面是下载
大家都知道,在portal中不好来设置content type,无法获得输出流.最好的方式就是在doview()中通过iframe在servlet中实现.
在这里我是用文件的形势实现下载的.
////////////////////////////////////////////////////////////////////////////////////////////////////
//*********************************************portlet 下载****************************************************************
public void init(PortletConfig config) throws PortletException{
super.init(config);
classes_path = config.getPortletContext().getRealPath("/WEB-INF/classes");
}
/**
* Serve up the <code>view</code> 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("<b>NO PORTLET SESSION YET</b>");
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[i]+"\\";
}
}
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();
%>
<p><font color="red">系统帮助文档下载</font></p>
<%
if(systemfiles!=null){
for(int i=0; i<systemfiles.length;i++){
%><p> <a href ="<%=renderResponse.encodeURL(renderRequest.getContextPath()) %>/systemhelp/<%=systemfiles[i] %>"><%=systemfiles[i] %></a></p><%
}
}
%>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
这就基本实现了下载.如果是图片就直接用ie打开.如果是word文档就弹出下载的对话框.
转自http://antheahuimin.blog.163.com/blog/static/180691320077301154094/
发表评论
-
eclipse单个程序的jvm设置
2009-02-23 11:06 1842eclipse 有启动参数里设置jvm大小,因为eclipse ... -
[Portlet]tabs标签引用
2009-02-20 12:02 1338在liferay中封装了丰富的UI组件,其中一个比较常用的就是 ... -
JAVA上传图片时获取尺寸
2009-02-19 12:04 2834BufferedImage sourceImg = javax ... -
关于url编码问题的处理的几个方法的总结
2009-02-12 14:47 1007有同事讨论到关于url编码的问题。 因此总结以下几点方法供大家 ... -
java读取配置文件的几种方法
2009-02-12 14:27 1258在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选 ... -
tomcat的OutOfMemoryError解决方法
2009-02-11 14:22 746最近在熟悉一个开发了有几年的项目,需要把数据库从mysql移植 ... -
zip解压
2009-02-11 14:17 972private static void extZi ... -
common-net包的ftp上传进度
2009-01-19 21:19 1789//上传文件 public void uploadFil ... -
用JakartaCommon的net组件来做ftp 文件传输(补充)
2009-01-12 14:21 809ftpClient.setControlEncoding(&q ... -
用JakartaCommon的net组件来做ftp 文件传输
2009-01-12 14:16 1218package com.wwkj.cms.test.ftp; ... -
tomcat传递url中文参数乱码
2008-12-26 15:36 811如果页面的编码正确,修改server.xml的connecto ... -
Hibernate的lazy属性失效
2008-12-26 15:28 939配置openSessionInViewFilter <f ... -
DWR session error
2008-12-26 15:25 842dwr不允许跨域访问,可在dwrservlet中加入 < ... -
获取时间
2008-12-26 15:00 545Calendar calendar = Calendar.ge ... -
excel汇出
2008-12-26 14:51 905输出html代码时,设置content-type为applic ...
相关推荐
在实际项目中,"Struts2PortletV1.0Demo"的部署过程涉及到将portlet打包成WAR文件,然后将其上传到支持JSR 168的portlet容器(如Pluto)中。运行时,用户可以通过浏览器访问portlet,执行CRUD操作,体验Struts2与...
3. **Portlet标准**:支持JSR 168和JSR 286(Web服务portlet API),兼容第三方portlet。 4. **AJAX技术**:提升用户体验,实现动态更新和交互。 四、开发过程 1. **规划**:确定门户的目标、内容、用户群体和功能...
**Liferay Portal** 是一款开源的企业级门户平台,它基于Java技术构建,支持多种标准,包括JSR 168和WSRP等。Liferay Portal 提供了一个高度可定制化的框架,允许开发者根据业务需求进行扩展和二次开发。 #### 二、...
- **功能**:JBoss Portal提供了portlet的管理和部署,支持多种portlet标准,如JSR-168和JSR-286。 - **配置**:JBoss Portal允许自定义布局,通过portlet布置,实现个性化门户页面。 3. **文件上传原理** - **...
6. **portlet开发**:开发者可以使用JSR-168或JSR-286标准来开发portlet,这些portlet可以是Java应用程序,也可以是基于其他技术如HTML、JavaScript或GWT(Google Web Toolkit)的Web应用程序。 7. **部署与配置**...
portlet容器支持JSR-168和JSR-286规范,允许开发者使用Java编写portlet,并提供了portlet生命周期管理、安全性和性能优化。 2. **个性化**: JBoss Portal支持用户自定义门户布局和内容,可以根据用户角色、偏好或...
Portlets 是Web开发中的一个重要概念,特别是在构建...对于想要深入学习Portlet开发的读者,可以参考Portlet规范(JSR 168和JSR 286),以及Apache Pluto的文档和示例,这些资源将帮助你掌握Portlet开发的各个方面。
内含源码,经IBM websphere portal6测试,供大家参考
- **JSR 168**:JSR 168是Java Portlet API的一个版本,它为portlet开发者提供了一个标准化的框架,使得portlet可以在支持JSR 168规范的任何门户容器中运行。 - **WSRP**:Web Services for Remote Portlets(WSRP)...
JSR-286相比之前的JSR-168,增加了许多新特性,例如异步处理、事件处理和增强的portlet间通信等,提升了portlet的性能和交互性。 NotificationPortlet充分利用了JSR-286的特性,实现了通知的动态加载和实时更新。...
该文档主要面向具备一定JSR-168基础知识,对Spring和Spring Web MVC有一定了解的Portal开发人员。适合希望深化Spring Portlet MVC框架理解的开发者阅读。 #### 二、Spring Portlet MVC核心概念 **2.1 Spring ...
Portlet是Java Portlet API(JSR-168和JSR-286)定义的一种标准组件模型,用于构建可插入到门户中的交互式小应用程序。一个portlet可以看作是门户中的一个小窗口,用户可以通过门户页面上的portlet来访问不同的功能...
14. **JBWIKI**:作为JBoss实验室的一个项目,它符合JSR 168规范,常用于增强JBoss Portal项目。 15. **Elsie**:提供了丰富的特性,如简单的wiki标记语法、内容管理、版本控制、访问控制、I18N支持等,并通过IoC...
14. **JBWIKI**:作为JBoss实验室项目,遵循JSR 168规范,可用于增强JBoss Portal,提供企业级的Wiki解决方案。 15. **Elsie**:具备常见的Wiki特性,如内容管理、版本控制、访问控制和多语言支持,同时使用IoC实现...
- **portlet-class**:指定Portlet实现类,这里是使用Struts2提供的Jsr168Dispatcher。 - **init-param**:初始化参数,用于配置Struts2的命名空间及默认动作。 - **expiration-cache**:设置缓存有效期。 - **...