[Java] struts2和bootstrap-fileupload →→→→→进入此内容的聊天室

来自 , 2021-03-16, 写在 Java, 查看 112 次.
URL http://www.code666.cn/view/8a146f1a
  1. package com.clcud.fileupload;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.PrintWriter;
  6. import java.util.List;
  7. import javax.servlet.ServletContext;
  8. import org.apache.struts2.ServletActionContext;
  9. import com.opensymphony.xwork2.ActionSupport;
  10. public class FileUplode  extends ActionSupport{
  11.         private List<File> myfile;
  12.         private List<String> myfileContentType;
  13.         private List<String> myfileFileName;
  14.         private static final long serialVersionUID = 1L;
  15.         @Override
  16.         public String execute() throws Exception {
  17.                 System.out.println("------------------------------");
  18.                 System.out.println("========>"+myfile);
  19.                 System.out.println("========>"+myfileContentType);  //text/plain
  20.                 System.out.println("========>"+myfileFileName);     //英语.txt
  21.                 System.out.println("------------------------------");
  22.                 ServletContext context = ServletActionContext.getServletContext();
  23.                 for(int i = 0;i<myfile.size();i++){
  24.                         File f = myfile.get(i);
  25.                         String path = context.getRealPath("/"+myfileFileName.get(i)); //E:\J2EE\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Shop\肉菜.txt
  26.                         System.out.println(path);
  27.                         FileOutputStream out = new FileOutputStream(path);
  28.                         FileInputStream in = new FileInputStream(f);
  29.                         int len = 0;
  30.                         byte[] buffer = new byte[1024];
  31.                         while( (len = in.read(buffer))!=-1){
  32.                                 out.write(buffer, 0, len);
  33.                         }
  34.                         in.close();
  35.                         out.close();
  36.                 }
  37.                 //异步提交必须返回数据和return NONE
  38.                 PrintWriter out = ServletActionContext.getResponse().getWriter();
  39.                 out.print("{\"flog\":\"success\"}");
  40.                 return NONE;
  41.         }
  42.  
  43.         public List<File> getMyfile() {
  44.                 return myfile;
  45.         }
  46.  
  47.         public void setMyfile(List<File> myfile) {
  48.                 this.myfile = myfile;
  49.         }
  50.  
  51.         public List<String> getMyfileContentType() {
  52.                 return myfileContentType;
  53.         }
  54.  
  55.         public void setMyfileContentType(List<String> myfileContentType) {
  56.                 this.myfileContentType = myfileContentType;
  57.         }
  58.  
  59.         public List<String> getMyfileFileName() {
  60.                 return myfileFileName;
  61.         }
  62.  
  63.         public void setMyfileFileName(List<String> myfileFileName) {
  64.                 this.myfileFileName = myfileFileName;
  65.         }
  66.  
  67. }
  68.  
  69. //---------------------------------------------------------------------------------------------------------------------------------------------------------
  70. // 前端代码
  71. //---------------------------------------------------------------------------------------------------------------------------------------------------------
  72. <%@ page language="java" contentType="text/html; charset=UTF-8"
  73.     pageEncoding="UTF-8"%>
  74. <%
  75.         String path = request.getContextPath();
  76.         String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  77. %>
  78. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  79. <html>
  80.         <head>
  81.                 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  82.                 <title>Insert title here</title>
  83.                 <link rel="stylesheet" type="text/css" href="<%=basePath %>fileupload/css/bootstrap.min.css" />
  84.                 <link rel="stylesheet" type="text/css" href="<%=basePath %>fileupload/css/fileinput.min.css" />
  85.                 <script src="<%=basePath %>fileupload/js/jquery.3.0.js" type="text/javascript" charset="utf-8"></script>
  86.                 <script src="<%=basePath %>fileupload/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
  87.                 <script src="<%=basePath %>fileupload/js/fileinput.js" type="text/javascript" charset="utf-8"></script>
  88.                 <script src="<%=basePath %>fileupload/js/locales/zh.js" type="text/javascript" charset="utf-8"></script>
  89.         </head>
  90. <body>
  91.         <form>
  92.                 <!--
  93.                         multiple表示允许同时上传多个文件,class=“file-loading”表示标签的样式。这里很重要,如果class="file",则中文化不能生效。
  94.          -->
  95.                 <input type="file" name="myfile" id="file-Portrait1" multiple class="file-loading">
  96.         </form>
  97. </body>
  98. </html>
  99. <script type="text/javascript">
  100. $(function () {
  101.     var control = $('#file-Portrait1');
  102.     //初始化上传控件的样式
  103.     control.fileinput({
  104.         language: 'zh', //设置语言
  105.         uploadUrl: '/struts2/fileupload.do', //上传的地址
  106.         //allowedFileExtensions: ['jpg', 'gif', 'png'],//接收的文件后缀
  107.         showUpload: true, //是否显示上传按钮
  108.         showCaption: true,//是否显示标题
  109.         showPreview: true,//是否预览
  110.         dropZoneEnabled: true,//是否显示拖拽区域
  111.         uploadAsync: true, //异步上传
  112.         browseClass: "btn btn-primary", //按钮样式    
  113.         maxFileSize: 0,//单位为kb,如果为0表示不限制文件大小
  114.         //minImageWidth: 50, //图片的最小宽度
  115.         //minImageHeight: 50,//图片的最小高度
  116.         //maxImageWidth: 1000,//图片的最大宽度
  117.         //maxImageHeight: 1000,//图片的最大高度
  118.         //minFileCount: 0,
  119.         maxFileCount: 100, //表示允许同时上传的最大文件个数
  120.         enctype: 'multipart/form-data',
  121.         validateInitialCount:true,
  122.         previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
  123.         msgFilesTooMany: "选择上传的文件数量{n} 超过允许的最大数值{m}!"
  124.     });
  125.     control.on("fileuploaded", function(event, data, previewId, index) {
  126.                 console.log(event);
  127.                 console.log(data.response);//返回的数据
  128.                 console.log(previewId);
  129.                 console.log(index);
  130.     });
  131. });
  132. </script>
  133.  
  134.  
  135.  
  136. //---------------------------------------------------------------------------------------------------------------------------------------------------------
  137. struts2 配置文件
  138. //---------------------------------------------------------------------------------------------------------------------------------------------------------
  139. <?xml version="1.0" encoding="UTF-8" ?>
  140. <!DOCTYPE struts PUBLIC
  141.         "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
  142.         "http://struts.apache.org/dtds/struts-2.5.dtd">
  143. <struts>
  144.         <!-- 指定由spring负责action对象的创建
  145.         <constant name="struts.objectFactory" value="spring" />
  146.         -->
  147.                
  148.         <!-- 请求参数的编码方式 -->
  149.         <constant name="struts.i18n.encoding" value="UTF-8"/>
  150.         <!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开 -->
  151.         <constant name="struts.action.extension" value="action,do,go,qqi"/>
  152.         <!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开  -->
  153.         <constant name="struts.configuration.xml.reload" value="true"/>
  154.         <!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开  -->
  155.         <constant name="struts.devMode" value="true"/>
  156.         <!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭  -->
  157.         <constant name="struts.serve.static.browserCache" value="false" />
  158.         <!-- 是否开启动态方法调用 -->
  159.         <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
  160.    <package name="default2" namespace="/" extends="struts-default">
  161.                 <action name="fileupload" class="com.clcud.fileupload.FileUplode">
  162.                         <result name="success">/index.jsp</result>
  163.                         <interceptor-ref name="fileUpload">
  164.                                 <param name="maximumSize">5555000000</param><!-- 5555000000 1048576 -->
  165.                         </interceptor-ref>
  166.                         <interceptor-ref name="defaultStack"></interceptor-ref>
  167.                 </action>
  168.    </package>
  169. </struts>

回复 "struts2和bootstrap-fileupload"

这儿你可以回复上面这条便签

captcha