spring mvc的文件上传之优化

我看了下很多楼主的文章,各有各的特色,但我觉得都不是我所想要的,所以我就自己写了个较完整的demo


我通过ajax+springmvc实现的文件上传 ajax的技术并没有使用自身携带的AjaxFileUpload(个人觉得代码量太多)


用ajax实现,网页里的很多表单数据也想获得怎么办呢 我是这样实现的:

先看我的xxx.jsp页面的实现代码:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




Insert title here




 

 

   

资源名称:


   

选择文件


   
 

 


以上便是jsp里代码相关的设计

接下来便是controller里的设计编码啦(配置最后讲解)里面的相关注解我就不解释啦 会的,都应该知道什么意思

package com.azj.controllers;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
@Controller
@RequestMapping("/upload")
public class uploadController {
@RequestMapping(value="/toUpload",method = RequestMethod.POST)
public void doUpload(@RequestParam("name")String name,HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException{

                //传统的都是通过io流来实现文件的上传,效率肯定是极低的!这里我们是通过springmvc自己及封装的文件                //上传方法;效率,比较快;感兴趣的可以测试下,大文件的上传速度,比较下;

               //获取前端表单属性name传来的数据  并设计文件发存放位置
File fs=new File("D:/"+name);
               if(!fs.exists())fs.mkdir();
//解析前端传过来的上下文
CommonsMultipartResolver cmr=new CommonsMultipartResolver(request.getSession().getServletContext());
//判断是传过来的上下文是否包含文件内容 如果有 执行文件处理及相关上传的操作
try{
if(cmr.isMultipart(request)){
//request强制转换为spring mvc自身携带的MultipartHttpServletRequest 文件处理
MultipartHttpServletRequest mr=(MultipartHttpServletRequest) request;
//迭代包含的文件
Iterator iter=mr.getFileNames();
//获取文件
while(iter.hasNext()){
MultipartFile file = mr.getFile((String)iter.next());
if(file!=null){
String filename="demo"+file.getOriginalFilename();
String path="D:/"+name+"/"+filename;
File f=new File(path);
//传文件
file.transferTo(f);
response.getWriter().println("上传成功");
}
}
}}catch (Exception e) {
response.getWriter().println("上传失败");
}


}
}

以上便是controller里的代码内容

现在说下相关配置

web.xml的内容如下:




  spring-mvc


    
 
    springmvc
    org.springframework.web.servlet.DispatcherServlet
   
      contextConfigLocation
      classpath*:config/applicationContext-mvc.xml
   

    1
 

 
    springmvc
    *.do
 



接下来是spring mvc的配置内容:

 
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:context="http://www.springframework.org/schema/context"    
    xmlns:mvc="http://www.springframework.org/schema/mvc"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.2.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  
  
     
     
  
     
     
  
     
     
         
         
         
   

    
     
     
         
           
           
           
   

   

以上面试springmvc上传的代码编码设计!



你可能感兴趣的:(spring,mvc)