利用SSM框架和ajax上传文件
a.jsp
需要引入ajaxfileupload.js这个文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"></c:set>
<!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>
</head>
<body>
<form id="queryForm" name="queryForm" action="" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="file" />
<button onclick="imageUpload()">提交</button>
</form>
<%-- <%=request.getServletContext().getRealPath("/") %>
--%></body>
<script src="${contextPath }/static/assets/js/jquery-1.10.1.js"></script>
<script src="${contextPath }/static/assets/js/jquery.min.js"></script>
<script type="text/javascript" src="${contextPath }/static/assets/js/jquery-1.12.4.js"></script>
<script src="${contextPath }/static/assets/js/ajaxfileupload.js" type="text/javascript"></script>
<script type="text/javascript">
function imageUpload(){
alert();
$.ajaxFileUpload({
url : '${contextPath }/uploadAction/uploadSong', //用于文件上传的服务器端请求地址
fileElementId : 'file', //文件上传空间的id属性 <input type="file" id="file" name="file" />
type : 'post',
dataType : 'text', //返回值类型 一般设置为json
success : function(data, status) //服务器成功响应处理函数
{
alert("歌曲上传成功");
},
error : function(data, status, e)//服务器响应失败处理函数
{
alert("歌曲上传失败");
}
});
}
</script>
uploadAction.java
package org.jsoft.ctrls;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
@Controller
@RequestMapping("uploadAction")
public class UploadAction {
/**
* 歌曲上传
* @return
* @throws IOException
*/
@RequestMapping(value = "uploadSong", method = RequestMethod.POST)
@ResponseBody
public String uploadSong(HttpServletRequest request, HttpServletResponse response, ModelMap model,HttpSession session) throws IOException {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile mFile = multipartRequest.getFile("file");
String path="E:\\";//替换成你所要保存的文件的位置
String filename = mFile.getOriginalFilename();
System.err.println("filename:"+filename);
InputStream inputStream = mFile.getInputStream();
byte[] b = new byte[1048576];
int length = inputStream.read(b);
String url =path + filename;
System.err.println(url);
FileOutputStream outputStream = new FileOutputStream(url);
outputStream.write(b, 0, length);
inputStream.close();
outputStream.close();
return filename;
}
}
总结
1.项目进度没有进展,但是SSM和ajax上传文件写完了,但是表单里的普通数据传到后台为空,这个需要更改引入的js文件源码
2.按条件搜索一直为空的问题解决了,汗,不是什么大问题,自己马虎了,没给传值和地址
3.按预约时间和签约时间排序时点击事件的问题====重新按照新增页的字典值思路来重新编写点击事件
本文介绍如何使用SSM框架结合Ajax实现文件上传功能,包括前端页面设计、Ajax调用及后端处理流程。同时提及了表单普通数据传递问题及解决思路。
172

被折叠的 条评论
为什么被折叠?



