private void writeResponse(String msg, HttpServletResponse response) throws Exception { response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); out.println(msg); out.flush(); out.close(); }
导入JS:<script src="<%=request.getContextPath()%>/common/js/utils/ajaxfileupload.js" type="text/javascript"></script>
<input id="myfiles" name="myfiles" type="file" /> <a class="blue-btn-coom-2 tcenter smibat" id="addBtn">确认</a>
$(function(){ $("#addBtn").click(function(){ addimg(); }); }); function addimg(){ //openBlock("",""); jQuery.blockUI(); $.ajaxFileUpload ( { url: $.tsp.contextPath + "/account/upload.do", //用于文件上传的服务器端请求地址 secureuri: false, //是否需要安全协议,一般设置为false fileElementId: "myfiles", //文件上传域的ID 多文件上传 id为数组 dataType: "json", //返回值类型 一般设置为json success: function (data) //服务器成功响应处理函数 { //alert(data.c); jQuery.unblockUI(); }, error: function (data, e)//服务器响应失败处理函数 { return alertMessage(e+"失败", "default"), !1; } } ) }
/** *练习图片上传 * @param file * @param request * @param model * @return * @throws Exception */ @RequestMapping(value = "/upload") public void upload(@RequestParam(value = "myfiles", required = false) MultipartFile myfiles, HttpServletRequest request,HttpServletResponse response) throws Exception { JSONObject oj = new JSONObject(); try { String path = request.getSession().getServletContext().getRealPath("upload"); String fileName = myfiles.getOriginalFilename(); //取得根目录路径 String rootPath=getClass().getResource("/").getFile().toString(); System.out.println("root"+rootPath); File targetFile = new File(path, fileName); String allPath = path+fileName; if(!targetFile.exists()){ targetFile.mkdirs(); } try { myfiles.transferTo(targetFile); if(targetFile.exists()){ //将文件存到数rnd System.out.println("All_Path"+path+fileName); } } catch (Exception e) { e.printStackTrace(); } oj.put("c", allPath); response.setContentType("text/html"); // response.getWriter().write(oj.toString()); writeResponse(oj.toString(), response); } catch (Exception e) { e.printStackTrace(); }finally { writeResponse(oj.toString(), response); } } private void writeResponse(String msg, HttpServletResponse response) throws Exception { response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); out.println(msg); out.flush(); out.close(); }
相关推荐
在这个场景中,我们关注的是如何利用Spring MVC实现文件的上传和下载功能,并且特别提到了`ajaxfileupload.js`这个JavaScript库。 文件上传是Web应用中的常见需求,Spring MVC提供了方便的API来处理这类操作。首先...
在JavaScript中,我们使用ajaxfileupload.js的API来监听文件上传事件,并发送Ajax请求到服务器。 ```javascript $(document).ready(function() { $("#uploadButton").click(function() { $.ajaxFileUpload({ url...
在Spring MVC中,我们可以使用`@RequestParam("file") MultipartFile file`注解来接收上传的文件。`MultipartFile`是Spring为处理多部分表单数据(如文件上传)而提供的接口。你需要确保服务器端有足够权限来写入...
SpringMVC是Java Web开发中的一个强大框架,而AJAXFileUpload则是一个用于异步文件上传的JavaScript库,能够提供用户友好的体验。 ### SpringMVC 框架简介 SpringMVC是Spring框架的一个模块,专门用于构建Web应用...
在 Spring MVC 框架中,这通常是一个带有 `@RequestMapping` 注解的方法,接收 `HttpServletRequest` 和 `HttpServletResponse` 参数,以便读取上传的文件和响应客户端。例如: ```java @SuppressWarnings(...
在Spring MVC中,可以使用`@RequestParam`注解来获取文件和其他参数: ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org....