java web srpingmvc 上传文件,大于1MB的文件上传途中总是中断,求赐教

isevenun 发布于 2017/02/02 08:57
阅读 962
收藏 2

目前用的框架是spring springmvc mybatis,我上传文件的时候,稍大点的文件在本地测试,当然瞬间就能上传成功,但是放到服务器上测试的时候,稍大点的文件上传需要耗费时间,所以常常会没办法上传成功。我上网找了个能显示进度条的代码,也试了下,还是一样的状况,上传到50-70%就终止了。求指教该如何能够稳定上传文件(图片)。我的代码如下:

前端代码:

<%@ page pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<title>test</title>
function UpladFile() {
    var fileObj = document.getElementById("file").files[0]; // js 获取文件对象
    var FileController = ""; // 接收上传文件的后台地址 

    // FormData 对象
    var form = new FormData();
    form.append("author", "hooyes"); // 可以增加表单数据
    form.append("file", fileObj); // 文件对象

    // XMLHttpRequest 对象
    var xhr = new XMLHttpRequest();
    xhr.open("post", FileController, true);
    xhr.onload = function() {
        // alert("上传完成!");
    };
    
    document.getElementById('progressInfo').style.display = "";
    xhr.upload.addEventListener("progress", progressFunction, false);

    xhr.send(form);
}

function clearProgressInfo()
{
    document.getElementById('progressInfo').style.display='none';
    document.getElementById("progressBar").value = 0;
    document.getElementById("percentage").innerHTML = "";
}

function progressFunction(evt) {
    var progressBar = document.getElementById("progressBar");
    var percentageDiv = document.getElementById("percentage");
    if (evt.lengthComputable) {
        progressBar.max = evt.total;
        progressBar.value = evt.loaded;
        percentageDiv.innerHTML = Math.round(evt.loaded / evt.total * 100) + "%";
    }
}
</script>
</head>
<body>

<input type="file" id="file" name="myfile" onclick="clearProgressInfo()"/>  
<span id="progressInfo" style="display:none;">  
<progress id="progressBar" value="0" max="100"></progress>  
<span id="percentage"></span>  
</span><br/>  
<input type="button" onclick="UpladFile()" value="上传" />  

</body>
</html>

 

后台代码:

@RequestMapping(value = "/test_upload", method = POST)
    public String uploadFile2(@RequestParam("file") CommonsMultipartFile file,  
            HttpServletRequest request) throws IOException {  
        // 定义解析器去解析request  
        CommonsMultipartResolver mutilpartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
        //request如果是Multipart类型、  
        if (mutilpartResolver.isMultipart(request)) {  
            //强转成 MultipartHttpServletRequest  
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;  
            //获取文件名  
            Iterator<String> it = multiRequest.getFileNames();  
            
            while (it.hasNext()) {  
                //获取MultipartFile类型文件  
                MultipartFile fileDetail = multiRequest.getFile(it.next());  
                if (fileDetail != null) {  
                    String fileName = "demoUpload" + fileDetail.getOriginalFilename();  
                    String path = "C:/eee/" + fileName;  
                    File localFile = new File(path);  
                    if (!localFile.exists())
                        localFile.mkdirs();
                    //将上传文件写入到指定文件出、核心!  
                    fileDetail.transferTo(localFile);  
                    //非常重要、有了这个想做什么处理都可以  
                    //fileDetail.getInputStream();  
                }  
            }  
        }  
        return "/result";  
    }  
    
    @RequestMapping(value = "/test_upload", method = GET)
    public String rrrrr() {
        return "result";
    }

 

springmvc的配置文件,servlet-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
				        http://www.springframework.org/schema/cache
				        http://www.springframework.org/schema/cache/spring-cache.xsd
				        http://www.springframework.org/schema/task
				        http://www.springframework.org/schema/task/spring-task-4.1.xsd
				        http://www.springframework.org/schema/aop
				        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">

	<!-- 扫描controller -->
	<context:component-scan base-package="www.ww.w.controller">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
	
	<!-- 启动切面 -->
	
	<!-- 注解驱动 -->
	<mvc:annotation-driven />
	
	<!-- 上传文件 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8" />
		<!-- 最大文件大小,-1为不限制大小 -->
		<property name="maxUploadSize" value="-1" />
		<property name="resolveLazily" value="true"/>
	</bean>

	<!-- 处理静态资源的请求 -->
	<mvc:resources location="/WEB-INF/views/backend/resources/" mapping="/resources/**" />
	<mvc:resources location="/images/" mapping="/images/**" />
	<mvc:resources location="/upload/" mapping="/upload/**" />
	<mvc:resources location="/MP_verify_53P2egQckwCAAR13.txt" mapping="/MP_verify_53P2egQckwCAAR13.txt" />
	<mvc:resources location="/WEB-INF/views/css/" mapping="/ubt-css/**" />
	<mvc:resources location="/WEB-INF/views/audio/" mapping="/ubt-audio/**" />
	<mvc:resources location="/WEB-INF/views/images/" mapping="/ubt-images/**" />
	<mvc:resources location="/WEB-INF/views/js/" mapping="/ubt-js/**" />
	<mvc:resources location="/WEB-INF/views/wechat/css/" mapping="/wechat-css/**" />
	<mvc:resources location="/WEB-INF/views/wechat/images/" mapping="/wechat-images/**" />
	<mvc:resources location="/WEB-INF/views/wechat/js/" mapping="/wechat-js/**" />
	<mvc:resources location="/WEB-INF/views/wechat-buy/css/" mapping="/wechat-buy-css/**" />
	<mvc:resources location="/WEB-INF/views/wechat-buy/images/" mapping="/wechat-buy-images/**" />
	<mvc:resources location="/WEB-INF/views/wechat-buy/js/" mapping="/wechat-buy-js/**" />
	
	<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>   
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/admin/**"/>
			
			<!-- 一下都是不拦截的  -->
			<!-- <mvc:exclude-mapping path="/resources/**"/>
			<mvc:exclude-mapping path="/images/**"/>
			<mvc:exclude-mapping path="/upload/**"/> -->
			
			<bean class="www.ww.w.interceptor.AdminLoginInterceptor"></bean>
		</mvc:interceptor>
		<mvc:interceptor>
			<mvc:mapping path="/super_admin/**"/>
			<bean class="www.ww.w.interceptor.SuperAdminLoginInterceptor"></bean>
		</mvc:interceptor>
		<mvc:interceptor>
			<mvc:mapping path="/download"/>
			<mvc:mapping path="/download/**"/>
			<bean class="www.ww.w.interceptor.LoginInterceptor"></bean>
		</mvc:interceptor>
		<mvc:interceptor>
			<mvc:mapping path="/wechat/upload**"/>
			<mvc:mapping path="/wechat/index-my**"/>
			<bean class="www.ww.w.interceptor.WechatUploadInterceptor"></bean>
		</mvc:interceptor>
		<mvc:interceptor>
			<mvc:mapping path="/wechat/**"/>
			<mvc:mapping path="/wechat-buy/**"/>
			<mvc:exclude-mapping path="/wechat/callback**"/>
			<bean class="www.ww.w.interceptor.WechatInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors>
	
	
	<task:annotation-driven/> <!-- 定时器开关 -->
    <bean id="orderJob" class="www.ww.w.job.OrderJob" />
    <task:scheduled-tasks>
    	<!-- Seconds Minutes Hours DayofMonth Month DayofWeek [Year]  -->
    	<!-- ,-*/    ,-*/     ,-*/  ,-*/?LWC  ,-*/   ,-*/LWC   ,-*/  -->
    	
    	<!-- 关闭指定时间内没有付款的订单  -->
    	<task:scheduled ref="orderJob" method="checkNoPayedNoClosedOrder" 
    		cron="0/4 * * * * ?"/>
    </task:scheduled-tasks>

</beans>

 

就这样卡在这里:

加载中
0
isevenun
isevenun

已经找到问题了,因为我在服务器用了gninx反向代理软件,而因为没有设置缓存,所以默认的缓存比较小,导致大于默认缓存大小的文件上传时会中途刷新缓存而导致上传失败。

w
wwlwtao
那你后来是解决是重新设置了哪个文件的哪个配置字段呀,能说一下不,谢谢啦
0
重生九死一生
重生九死一生
把-1改成10m试一试。
isevenun
isevenun
回复@重生九死一生 : 不知道是服务器不稳定还是什么问题?
isevenun
isevenun
回复@重生九死一生 : 您好,我试了还是一样,上传一半就卡住了。
0
MnameHZJ
MnameHZJ
try catch打印一下日志
MnameHZJ
MnameHZJ
或者你打个断点跟踪一下,看看执行到哪里停止,程序到哪运行不下去的
isevenun
isevenun
回复@MnameHZJ : 您好,我试了下在后台里try catch,然后在tomcat的日志里没有发现有错误输出,即使没有try catch,我在tomcat stderr日志文件里也没发现有说错误
返回顶部
顶部