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();
}
...全文
1174 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
戎码一生灬 2018-02-01
  • 打赏
  • 举报
回复
入参那里用@RequestParam("file") MultipartFile file来接收
Sunyiban 2018-01-31
  • 打赏
  • 举报
回复
你request取文件的地方在哪呢。。我咋没看到
qq_40881558 2018-01-26
  • 打赏
  • 举报
回复
[yabao=啊啊啊啊啊啊][/yabao]
tianfang 2018-01-03
  • 打赏
  • 举报
回复
错误表现是什么?错误信息是什么?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧