博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring mvc上传下载文件
阅读量:5825 次
发布时间:2019-06-18

本文共 13033 字,大约阅读时间需要 43 分钟。

前端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 + "/";%>
文件上传

表单提交的方式上传文件

file:
id:

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

text/plain;charset=UTF-8

 

转载地址:http://fusdx.baihongyu.com/

你可能感兴趣的文章
ubuntu 11.10 rsync 服务配置
查看>>
WP8开发系列3:WP7与WP8在文件IO操作上的区别
查看>>
Linux系统下安装rz/sz命令及使用说明
查看>>
接口概述学习
查看>>
简单php文件编写语法
查看>>
python的多线程中的join的作用
查看>>
pycharm中Django在html文件里面使用模板语言
查看>>
The method setOnClickListener(View.OnClickListener)解决办法
查看>>
LCD1602 显示数字,字符,自定义字符,字符串,光标
查看>>
我的 vim 配置
查看>>
win7修改默认输入法
查看>>
一次ARP问题的Troubleshooting
查看>>
mysql 变量
查看>>
性能测试中传——lr用法理论(五)
查看>>
c++写 2048
查看>>
大数据Java基础第四天作业
查看>>
redis集群搭建
查看>>
存储的单位
查看>>
iOS框架MVC+MVVM结合的实战
查看>>
mysql error :This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA
查看>>