SSM上传文件时候后台request没有获取到file?
饿死的胖子 2017-12-30 09:55:45 //前台jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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 href="${pageContext.request.contextPath }/statics/js/uploadify.css" rel="stylesheet" type="text/css" />
<script src="${pageContext.request.contextPath }/statics/js/jquery-1.11.3.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath }/statics/js/jquery.uploadify.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
var path=$("#path").val();
$("#uploadify").uploadify({
'swf' : path+'/statics/js/uploadify.swf',
'uploader' : path+'/sys/upload/uploads',
'folder' : '/upload',
'queueID' : 'fileQueue',
'cancelImg' : 'plugin/uploadify/uploadify-cancel.png',
'buttonText' : '上传文件',
'auto' : false, //设置true 自动上传 设置false还需要手动点击按钮
'multi' : true,
'wmode' : 'transparent',
'simUploadLimit' : 999,
'fileTypeExts' : '*.*',
'fileTypeDesc' : 'All Files'
});
});
</script>
</head>
<body>
<div>
<%--用来作为文件队列区域--%>
<div id="fileQueue" style="position:absolute; right:50px; bottom:100px;z-index:999">
</div>
<input type="hidden" value="${pageContext.request.contextPath }" id="path"/>
<input type="file" name="uploadify" id="uploadify"/>
<p>
<a href="javascript:$('#uploadify').uploadify('upload','*')">上传</a>|
<a href="javascript:$('#uploadify').uploadify('cancel','*')">取消上传</a>
</p>
</div>
</body>
</html>
/*后台*/
@RequestMapping(value = "/uploads")
public void upload(HttpServletRequest request, HttpServletResponse response) throws IOException{
// TODO Auto-generated method stub
System.out.println("----------------------------------------------------------------");
System.out.println(request.getRequestURL());
System.out.println(request.getRequestURI());
System.out.println(request.getContextPath());
System.out.println(request.getServletPath());
System.out.println(request.getQueryString());
//文件存放的目录
File tempDirPath =new File(request.getSession().getServletContext().getRealPath("/")+"\\upload\\");
String sdad=request.getSession().getServletContext().getRealPath("/")+"\\upload\\";
System.out.println(sdad+"------------ddddddddddddddddddddddddddddddddddddddd");
System.out.println(tempDirPath);
if(!tempDirPath.exists()){
tempDirPath.mkdirs();
}
//创建磁盘文件工厂
DiskFileItemFactory fac = new DiskFileItemFactory();
//创建servlet文件上传组件
ServletFileUpload upload = new ServletFileUpload(fac);
//文件列表
List<FileItem> fileList = null;
//解析request从而得到前台传过来的文件
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException ex) {
ex.printStackTrace();
return;
}
//保存后的文件名
String imageName = null;
//便利从前台得到的文件列表
Iterator<FileItem> it = fileList.iterator();
while(it.hasNext()){
FileItem item = it.next();
//如果不是普通表单域,当做文件域来处理
if(!item.isFormField()){
imageName = new Date().getTime()+Math.random()*10000+item.getName();
BufferedInputStream in = new BufferedInputStream(item.getInputStream());
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File(tempDirPath+"\\"+imageName)));
Streams.copy(in, out, true);
}
}
//
PrintWriter out = null;
try {
out = encodehead(request, response);
} catch (IOException e) {
e.printStackTrace();
}
//这个地方不能少,否则前台得不到上传的结果
out.write("1");
out.flush();
out.close();
}