在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 1799eclipse 有启动参数里设置jvm大小,因为eclipse ... -
[Portlet]tabs标签引用
2009-02-20 12:02 1302在liferay中封装了丰富的UI组件,其中一个比较常用的就是 ... -
JAVA上传图片时获取尺寸
2009-02-19 12:04 2770BufferedImage sourceImg = javax ... -
关于url编码问题的处理的几个方法的总结
2009-02-12 14:47 947有同事讨论到关于url编码的问题。 因此总结以下几点方法供大家 ... -
java读取配置文件的几种方法
2009-02-12 14:27 1221在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选 ... -
tomcat的OutOfMemoryError解决方法
2009-02-11 14:22 721最近在熟悉一个开发了有几年的项目,需要把数据库从mysql移植 ... -
zip解压
2009-02-11 14:17 904private static void extZi ... -
common-net包的ftp上传进度
2009-01-19 21:19 1760//上传文件 public void uploadFil ... -
用JakartaCommon的net组件来做ftp 文件传输(补充)
2009-01-12 14:21 771ftpClient.setControlEncoding(&q ... -
用JakartaCommon的net组件来做ftp 文件传输
2009-01-12 14:16 1168package com.wwkj.cms.test.ftp; ... -
tomcat传递url中文参数乱码
2008-12-26 15:36 769如果页面的编码正确,修改server.xml的connecto ... -
Hibernate的lazy属性失效
2008-12-26 15:28 892配置openSessionInViewFilter <f ... -
DWR session error
2008-12-26 15:25 805dwr不允许跨域访问,可在dwrservlet中加入 < ... -
获取时间
2008-12-26 15:00 511Calendar calendar = Calendar.ge ... -
excel汇出
2008-12-26 14:51 847输出html代码时,设置content-type为applic ...
相关推荐
用WebLogic Portal 8.1 开发 JSR 168 Portlets
jsr168和jsr268中文文档和开发手册; jsr168和jsr268中文文档和开发手册
jsr168开发的一些经验..
对于门户组件的开发,业界中目前都是基于JSR168的规范开发,因为英文文档理解起来比较拗口,有相关好人进行了翻译,非常感谢这些人。
JSR168规范与API手册 官方手册、标准 Portlet1.0开发必备
基于JSR168的portlet精彩范例
本压缩包里含有了开发一个jsr168 portlet所需要的软件 本想包含jetspeed2.0的安装程序的,可是最多智能上传10M <br>从环境配置讲到开发步骤。 并表明了很多注意的地方 本包适合初学portlet的人使用
PORTAL开发相关标准:JSR168Introduction_to_JSR_168
目前最全的资源 jsr168 jsr286 中英文都有 还有API
JSR168 portlet portal
portlet国际标准 JSR168 PORLET标准手册汉化整理
JSR-168 中文版
JSR-168是适合于portlet开发人员的Java API集合。设计符合规范的JSR-168 portlet的原因有很多。可移植性就一个显而易见的好处。根据规范编写的代码更容易在门户服务器之间移动。多数基于Java的门户服务器都支持JSR-...
我利用ibm提供的标准portlet改写的一个投票和网上调查,其中两者后台在edit模式中,里面还用到了js饼图统计,非常适合初学者
中文翻譯的JSR168 PORLET標準,講述了什么是Portal以及portal的架構及JSR168 PORLET標準等等
portal portlet j2ee websphere ibm
JSR168 PORLET标准手册JSR168 PORLET标准手册