• 大小: 7.31M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2025-05-03
  • 语言: 其他
  • 标签: 其他  

资源简介

SpringMVC文件上传与下载的实现.rar

资源截图

代码片段和文件信息

package com.mlxp.controller;

import java.io.File;
import java.io.OutputStream;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.mlxp.entity.User;

@Controller
@RequestMapping(“/hellorabbit/*.do“)
public class DownloadController {
@RequestMapping
public String hellomlxp(){
System.out.println(“hellomlxp“);
return “upload“;
}
@RequestMapping
public String hellouser(){
System.out.println(“hellouser“);
return “register“;
}
@RequestMapping(method=RequestMethod.POST)
    public String upload(HttpServletRequest request
           @RequestParam(“description“) String description
           @RequestParam(“file“) MultipartFile fileModel model) throws Exception {

        System.out.println(description);
//      //不使用注解
// MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// //获取file
// MultipartFile file = multipartRequest.getFile(“file“);
        //如果文件不为空,写入上传路径
        if(!file.isEmpty()) {
//            //上传文件路径
//            String path = request.getSession().getServletContext().getRealPath(“/ooxx/“);
         //由配置文件获取
         String path=(String) request.getSession().getServletContext().getAttribute(“path“);
            //上传文件名
            String filename = file.getOriginalFilename();
            File filepath = new File(pathfilename);
            //判断路径是否存在,如果不存在就创建一个
            if (!filepath.getParentFile().exists()) { 
                filepath.getParentFile().mkdirs();
            } 
//            // 读取要下载的文件,保存到文件输入流
//            InputStream in = file.getInputStream();
//            // 创建输出流
//            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filepath));
//            // 创建缓冲区
//            byte[] buffer = new byte[32 * 10 * 1024];
//     int bytesRead = 0;
//     // 循环将输入流中的内容读取到缓冲区当中
//     while ((bytesRead = in.read(buffer)) != -1){
//     // 输出缓冲区的内容到浏览器,实现文件下载
//         out.write(buffer 0 bytesRead);
//     }
//     // 关闭输出流
//            out.close();
//            // 关闭文件输入流
// in.close();
            //将上传文件保存到一个目标文件当中
            file.transferTo(new File(path + File.separator + filename));
            model.addAttribute(“filename“ filename);
            return “download

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        852  2017-09-26 13:30  Download_demo\.classpath

     文件       1046  2017-09-21 13:35  Download_demo\.project

     文件        517  2017-09-26 13:30  Download_demo\.settings\.jsdtscope

     文件        364  2017-09-15 13:59  Download_demo\.settings\org.eclipse.jdt.core.prefs

     文件        117  2017-09-21 13:36  Download_demo\.settings\org.eclipse.jst.j2ee.ejb.annotations.xdoclet.prefs

     文件        463  2017-09-26 13:30  Download_demo\.settings\org.eclipse.wst.common.component

     文件        345  2017-09-26 13:30  Download_demo\.settings\org.eclipse.wst.common.project.facet.core.xml

     文件         49  2017-09-15 13:59  Download_demo\.settings\org.eclipse.wst.jsdt.ui.superType.container

     文件          6  2017-09-15 13:59  Download_demo\.settings\org.eclipse.wst.jsdt.ui.superType.name

     文件       6319  2017-09-26 13:39  Download_demo\build\classes\com\mlxp\controller\DownloadController.class

     文件        909  2017-09-26 13:39  Download_demo\build\classes\com\mlxp\entity\User.class

     文件       2296  2017-09-26 13:39  Download_demo\build\classes\com\mlxp\listener\PropertiesListener.class

     文件         13  2017-09-26 11:16  Download_demo\build\classes\path.properties

     文件       2105  2017-09-26 13:30  Download_demo\build\classes\spring-mvc.xml

     文件        843  2017-09-26 13:30  Download_demo\build\classes\spring.xml

     文件       6913  2017-09-26 13:39  Download_demo\src\com\mlxp\controller\DownloadController.java

     文件        635  2017-09-21 15:11  Download_demo\src\com\mlxp\entity\User.java

     文件       1769  2017-09-26 13:30  Download_demo\src\com\mlxp\listener\PropertiesListener.java

     文件         13  2017-09-26 11:16  Download_demo\src\path.properties

     文件       2105  2017-09-26 13:30  Download_demo\src\spring-mvc.xml

     文件        843  2017-09-26 13:30  Download_demo\src\spring.xml

     文件         86  2017-09-26 13:30  Download_demo\WebContent\index.jsp

     文件      78601  2015-01-02 15:00  Download_demo\WebContent\js\jquery-1.4.4.min.js

     文件         39  2017-09-15 13:59  Download_demo\WebContent\meta-INF\MANIFEST.MF

     文件     188671  2015-01-02 15:59  Download_demo\WebContent\WEB-INF\lib\commons-beanutils-1.7.0.jar

     文件      46725  2015-01-02 15:59  Download_demo\WebContent\WEB-INF\lib\commons-codec-1.3.jar

     文件     571259  2015-01-02 15:59  Download_demo\WebContent\WEB-INF\lib\commons-collections-3.2.jar

     文件      71442  2015-01-02 15:59  Download_demo\WebContent\WEB-INF\lib\commons-discovery-0.2.jar

     文件      59590  2015-01-02 15:59  Download_demo\WebContent\WEB-INF\lib\commons-fileupload-1.2.2.jar

     文件     278799  2015-01-02 15:59  Download_demo\WebContent\WEB-INF\lib\commons-httpclient-3.0-rc4.jar

............此处省略55个文件信息

评论

共有 条评论