前端jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%>文件上传 表单提交的方式上传文件
ajax文件上传方式
file:
前端js
/** * 页面加载之后执行的函数;===================================== */$(function() {});/** * 元素事件注册函数;===================================== */$(function() { $('#btn01Id').click(btn01IdClick);});/** * 全局变量定义部分;===================================== */var i = 0;/** * 事件编写部分;===================================== */function btn01IdClick(e) { if(!$("#file1").val()){ return ; } var sendData = { "id" : 123 }; $.ajaxFileUpload({ url:'file/upload.do', secureuri:false, fileElementId:'file1', data : sendData, timeOut : 5000, success: function (data, status){ alert("上传成功!"); }, error: function (data, status, e){ alert("上传出错:"+e); } }); }
ajaxfileuploadjs
jQuery.extend({ createUploadIframe: function(id, uri) { //create frame var frameId = 'jUploadFrame' + id; var iframeHtml = '
后端controller
package com.srie.fileud.controller;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.multipart.MultipartFile;@Controller@RequestMapping("/file")public class FileUD { @ResponseBody @RequestMapping("/upload") public String uploadFile(HttpServletRequest request, MultipartFile file1, Integer id) { System.out.println("id is : "+id); String name = file1.getName(); System.out.println("name : "+name); String contentType = file1.getContentType(); System.out.println("contentType : "+contentType); String originalFilename = file1.getOriginalFilename(); System.out.println("originalFilename: "+originalFilename); long size = file1.getSize(); System.out.println("size: "+size); boolean empty = file1.isEmpty(); System.out.println("empty: "+empty); File saveFile = new File(request.getServletContext().getRealPath("/file"), originalFilename); try { file1.transferTo(saveFile); } catch (Exception e) { e.printStackTrace(); } return "asdf"; } @ResponseBody @RequestMapping(value="/download") public String downFile(HttpServletRequest request, HttpServletResponse response){ String directory = request.getServletContext().getRealPath("/file"); File file = new File(directory,"123"); if(file.exists()){ response.setContentType("application/octet-stream;charset=ISO8859-1"); String name = "《中文》09aa囿気.docx"; String name2 = "a.docx"; try { name2 = new String(name.getBytes(),"ISO8859-1"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } response.addHeader("content-disposition", "attachment;filename="+name2); byte[] buffer = new byte[1024]; FileInputStream fis = null; BufferedInputStream bis = null; try { fis = new FileInputStream(file); bis = new BufferedInputStream(fis); OutputStream os = response.getOutputStream(); int i = bis.read(buffer); while(i!=-1){ os.write(buffer,0,i); i = bis.read(buffer); } } catch (Exception e) { e.printStackTrace(); }finally { if(bis!=null){ try { bis.close(); } catch (Exception e2) { e2.printStackTrace(); } } if(fis != null){ try { fis.close(); } catch (Exception e2) { e2.printStackTrace(); } } } } return null; }}
web.xml
fileud org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:beans.xml springMVC org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc.xml 1 springMVC *.do characterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 characterEncodingFilter *.do index.html index.htm index.jsp default.html default.htm default.jsp spring.profiles.active dev spring.profiles.default dev spring.liveBeansView.mbeanDomain dev
springmvc.xml