fileupload实现Portlet上传文件

下载两个第三方开发包: commons-fileupload commons-io(commons-fileupload引用).

官方下载:

commons-fileupload-1.2.jar

http://commons.apache.org/downloads/download_fileupload.cgi

 

commons-io-1.3.2

http://commons.apache.org/downloads/download_io.cgi

 

Portlet相关代码:

 

 

页面JSP代码:

 

需要注意的地方:

1.

对于Portlet:

import org.apache.commons.fileupload.portlet.PortletFileUpload;

dfif.setRepository(new File(portletContext.getRealPath("/UploadFileTemp")));

String u_name = portletContext.getRealPath("/") + "/UploadFile/" + prefix + "." + t_ext;

 

如果是一般的J2EE Web工程,就需要对上面各点做如下修改:

import org.apache.commons.fileupload.servlet.ServletFileUpload;

dfif.setRepository(new File(request.getRealPath("/") + "UploadFileTemp")));

String u_name = request.getRealPath("/") + " UploadFile /" + prefix + "." + t_ext;

 

打开commons-fileupload-1.2.jar,你会发现它已经包含了PortletRequestServletRequest的相应处理

org.apache.commons.fileupload.portlet

org.apache.commons.fileupload.servlet

方便PortalWeb Project的开发。

 

2.

对于方法 .getRealPath(String path)的使用需要特别注意。当你所指向的文件夹或文件是存在于.ear或者.war中时,此方法不会返回其绝对路径,而是返回null

这个时候,你需要将你的.ear或者.war使用Java jar xvf *.ear命令解压开安装到相应的Web Server。或者将自己想存放的路径指向Server的其他路径。

 

getRealPath

java.lang.String getRealPath(java.lang.String path)

Returns a String containing the real path for a given virtual path. For example, the path /index.html returns the absolute file path of the portlet container file system.

The real path returned will be in a form appropriate to the computer and operating system on which the portlet container is running, including the proper path separators. This method returns null if the portlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).

Parameters:

path - a String specifying a virtual path

Returns:

a String specifying the real path, or null if the transformation cannot be performed.

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值