用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

struts2和bootstrap-fileupload

2017-03-17 作者: ch举报

[java]代码库

package com.clcud.fileupload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUplode  extends ActionSupport{
	private List<File> myfile;
	private List<String> myfileContentType;
	private List<String> myfileFileName;
	private static final long serialVersionUID = 1L;
	@Override
	public String execute() throws Exception {
		System.out.println("------------------------------");
		System.out.println("========>"+myfile);
		System.out.println("========>"+myfileContentType);  //text/plain
		System.out.println("========>"+myfileFileName);     //英语.txt
		System.out.println("------------------------------");
		ServletContext context = ServletActionContext.getServletContext();
		for(int i = 0;i<myfile.size();i++){
			File f = myfile.get(i);
			String path = context.getRealPath("/"+myfileFileName.get(i)); //E:\J2EE\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Shop\肉菜.txt
			System.out.println(path);
			FileOutputStream out = new FileOutputStream(path);
			FileInputStream in = new FileInputStream(f);
			int len = 0;
			byte[] buffer = new byte[1024];
			while( (len = in.read(buffer))!=-1){
				out.write(buffer, 0, len);
			}
			in.close();
			out.close();
		}
		//异步提交必须返回数据和return NONE
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		out.print("{\"flog\":\"success\"}");
		return NONE;
	}

	public List<File> getMyfile() {
		return myfile;
	}

	public void setMyfile(List<File> myfile) {
		this.myfile = myfile;
	}

	public List<String> getMyfileContentType() {
		return myfileContentType;
	}

	public void setMyfileContentType(List<String> myfileContentType) {
		this.myfileContentType = myfileContentType;
	}

	public List<String> getMyfileFileName() {
		return myfileFileName;
	}

	public void setMyfileFileName(List<String> myfileFileName) {
		this.myfileFileName = myfileFileName;
	}

}

//---------------------------------------------------------------------------------------------------------------------------------------------------------
// 前端代码
//---------------------------------------------------------------------------------------------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Insert title here</title>
		<link rel="stylesheet" type="text/css" href="<%=basePath %>fileupload/css/bootstrap.min.css" />
		<link rel="stylesheet" type="text/css" href="<%=basePath %>fileupload/css/fileinput.min.css" />
		<script src="<%=basePath %>fileupload/js/jquery.3.0.js" type="text/javascript" charset="utf-8"></script>
		<script src="<%=basePath %>fileupload/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="<%=basePath %>fileupload/js/fileinput.js" type="text/javascript" charset="utf-8"></script>
		<script src="<%=basePath %>fileupload/js/locales/zh.js" type="text/javascript" charset="utf-8"></script>
	</head>
<body>
	<form>
		<!--
			multiple表示允许同时上传多个文件,class=“file-loading”表示标签的样式。这里很重要,如果class="file",则中文化不能生效。
         -->
		<input type="file" name="myfile" id="file-Portrait1" multiple class="file-loading">
	</form>
</body>
</html>
<script type="text/javascript">
$(function () {
    var control = $('#file-Portrait1');
    //初始化上传控件的样式
    control.fileinput({
        language: 'zh', //设置语言
        uploadUrl: '/struts2/fileupload.do', //上传的地址
        //allowedFileExtensions: ['jpg', 'gif', 'png'],//接收的文件后缀
        showUpload: true, //是否显示上传按钮
        showCaption: true,//是否显示标题
        showPreview: true,//是否预览
        dropZoneEnabled: true,//是否显示拖拽区域
        uploadAsync: true, //异步上传
        browseClass: "btn btn-primary", //按钮样式     
        maxFileSize: 0,//单位为kb,如果为0表示不限制文件大小
        //minImageWidth: 50, //图片的最小宽度
        //minImageHeight: 50,//图片的最小高度
        //maxImageWidth: 1000,//图片的最大宽度
        //maxImageHeight: 1000,//图片的最大高度
        //minFileCount: 0,
        maxFileCount: 100, //表示允许同时上传的最大文件个数
        enctype: 'multipart/form-data',
        validateInitialCount:true,
        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
        msgFilesTooMany: "选择上传的文件数量{n} 超过允许的最大数值{m}!"
    });
    control.on("fileuploaded", function(event, data, previewId, index) {
		console.log(event);
		console.log(data.response);//返回的数据
		console.log(previewId);
		console.log(index);
    });
});
</script>



//---------------------------------------------------------------------------------------------------------------------------------------------------------
struts2 配置文件
//---------------------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<!-- 指定由spring负责action对象的创建 
	<constant name="struts.objectFactory" value="spring" />
	-->
		
	<!-- 请求参数的编码方式 -->
	<constant name="struts.i18n.encoding" value="UTF-8"/>
	<!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开 -->
	<constant name="struts.action.extension" value="action,do,go,qqi"/>
	<!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开  -->
	<constant name="struts.configuration.xml.reload" value="true"/>
	<!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开  -->
	<constant name="struts.devMode" value="true"/>
	<!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭  -->
	<constant name="struts.serve.static.browserCache" value="false" />
	<!-- 是否开启动态方法调用 -->
	<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
   <package name="default2" namespace="/" extends="struts-default">
   		<action name="fileupload" class="com.clcud.fileupload.FileUplode">
   			<result name="success">/index.jsp</result>
   		        <interceptor-ref name="fileUpload">
                                <param name="maximumSize">5555000000</param><!-- 5555000000 1048576 -->
                        </interceptor-ref>
                        <interceptor-ref name="defaultStack"></interceptor-ref>
   		</action>
   </package>
</struts>


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...