struts2 多个文件上传 插件goouploader_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > struts2 多个文件上传 插件goouploader

struts2 多个文件上传 插件goouploader

 2013/12/19 19:09:11  zhao103804  程序员俱乐部  我要评论(0)
  • 摘要:用struts2框架编写多个文件上传的代码,利用google插件的goouploader,该插件的界面美观,如下:用法如下:将该插件如果应用到你项目中去的话,需要注意一下路径问题在你的jsp中在展示图片上传index.jsp中的相关代码:<scripttype="text/javascript"src="<%=request.getContextPath()%>/js/GooUploader/jquery-1.8.0.min.js"></script><
  • 标签:上传 文件 插件 struts

struts2框架编写多个文件上传的代码,利用google插件的goouploader,该插件的界面美观,如下:


用法如下:

将该插件如果应用到你项目中去的话,需要注意一下路径问题

在你的jsp中在展示图片上传

index.jsp中的相关代码:

?

??<script? type="text/javascript" src="<%=request.getContextPath()%>/js/GooUploader/jquery-1.8.0.min.js"></script>
?<script? type="text/javascript" src="<%=request.getContextPath()%>/js/GooUploader/GooUploader.js"></script>
?<script type="text/javascript" src="<%=request.getContextPath()%>/js/GooUploader/swfupload/swfupload.js"></script>

<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/js/GooUploader/GooUploader.css"/>

?<script src="<%=request.getContextPath()%>/js/DataOperator.js"></script>??
?<script type="text/javascript">
??var _contextPath_ = "<%=_contextPath_%>";
?</script>

文件中需要定义变量_contextPath_,该值的来源为jsp头部的java代码,如下:
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
?String _contextPath_ = request.getContextPath().equals("/") ? "" : request.getContextPath();
%>

<body>

?????? <div id="morefiledodiv"></div>

</body>

?

DataOperator.js相关代码:

我这个在项目中的应用是点击事件调用的该函数

//多个文件上传的函数
/**
?* 在此处需要注意的是,flash_url的值为_contextPath_ +'',如果没有_contextPath_则点击插件中的按钮无反应
?* _contextPath_ 在index.jsp中的上面js中定义该变量的值
?*
?* */
function morefileupload(){
?var? post_params = {session_id:"f1423rwe543t4wrtwerwe"};
?var property2={
??width:250,
??height:240,
??multiple:true,
???? //file_types:"*.jpg;*.gif",
???? //file_types_description: "Web Image Files",
???? post_params:post_params,
???? btn_add_text:"添加",
???? btn_up_text:"上传",
???? btn_cancel_text:"放弃",
???? btn_clean_text:"清空",
???? op_del_text:"单项删除",
???? op_up_text:"单项上传",
???? op_fail_text:"上传失败",
???? op_ok_text:"上传成功",
???? op_no_text:"取消上传",
??upload_url:"mulFileUpload.action",
??flash_url :_contextPath_ +"/js/GooUploader/swfupload/swfupload.swf"
?};
?$.createGooUploader($("#morefiledodiv"),property2);
}

?

后台代码:

/**
?* @author qf
?*/?
@SuppressWarnings("serial")
public class UploadAction extends ActionSupport?
{?
?/** 文件对象 */
?private List<File> Filedata;
?/** 文件名 */
?private List<String> FiledataFileName;
?/** 文件内容类型 */
?private List<String> FiledataContentType;
?/** 返回字符串 */
?private String returnValue = null;
?
?public void mulFileUpload(){
??if(Filedata!=null){
???int size = Filedata.size();
???for (int i = 0; i < size; i++) {
????File file = Filedata.get(i);
????String path=ServletActionContext.getServletContext().getRealPath("/");
????String imgName=FiledataFileName.get(i);
????try {
?????FileUtils.copyFile(file,new File(path+"upload\\"+imgName));
?????String outPath=ServletActionContext.getRequest().getScheme()+"://"+ServletActionContext.getRequest().getServerName()+":"+ServletActionContext.getRequest().getServerPort()+ServletActionContext.getRequest().getContextPath()+"/";
?????ServletActionContext.getResponse().getWriter().print(outPath+"upload/"+imgName);
????} catch (IOException e) {
?????e.printStackTrace();
????}
???}
??}
?}
?
?public List<File> getFiledata() {
??return Filedata;
?}
?public void setFiledata(List<File> filedata) {
??Filedata = filedata;
?}
?public List<String> getFiledataFileName() {
??return FiledataFileName;
?}
?public void setFiledataFileName(List<String> filedataFileName) {
??FiledataFileName = filedataFileName;
?}
?public List<String> getFiledataContentType() {
??return FiledataContentType;
?}
?public void setFiledataContentType(List<String> filedataContentType) {
??FiledataContentType = filedataContentType;
?}
?public String getReturnValue() {
??return returnValue;
?}
?public void setReturnValue(String returnValue) {
??this.returnValue = returnValue;
?}?
}

?

struts.xml的相关代码:

?<constant name="struts.devMode" value="true" />
??? <constant name="struts.i18n.encoding" value="utf-8" />
??? <constant name="struts.devMode" value="false"/>
??? <constant name="struts.multipart.maxSize" value="20971520"/>

??? <package name="upload"? extends="struts-default">?
??????? <action name="mulFileUpload" class="org.action.UploadAction"? method="mulFileUpload"></action>?
??? </package>

?

将以上的代码应用到你项目中就可以运行了。。

?

这个跟goouploader中的例子中没有多大区别,应用到项目中的话,主要就是路径的问题,_contextPath_?这个变量的应用很重要,不然点击插件中的按钮是没有反应的。

?

上传的是多个文件上传的一个例子,可以直接导入到eclipse工具中运行

提示:(导入进去如果在java代码中出现ServletActionContext.getResponse()报错,则是你缺少javaee.jar包)

?

?

以上贴出来的代码跟上传的例子中的一样,只是应用到项目中去的话就需要多一个步骤,就是定义一个获取项目路径的变量

?



?

  • 大小: 54.2 KB
  • GooUploaderDemo.rar (4.5 MB)
  • 下载次数: 0
  • 查看图片附件
发表评论
用户名: 匿名