1.在springmvc.xml配置和文件中要加上

1   <!-- 文件上传  富媒体解析器-->               2    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

1.springmvc.xml代码如下

电脑培训,计算机培训,平面设计培训,网页设计培训,美工培训,Web培训,Web前端开发培训

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-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/aop 
                        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd    
                        
                        
                        ">

    <!-- 扫描mvc -->
    <context:component-scan base-package="com.bypx.controller" />
    <aop:aspectj-autoproxy proxy-target-class="true" />
    <!-- 开启mvc注解功能 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>text/plain;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
            
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!-- 视图解析器 -->
    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--前缀 -->
        <property name="prefix" value="" />
        <!--后缀 -->
        <property name="suffix" value="" />
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
    </bean>

     <!-- 文件上传  富媒体解析器-->               
   <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>        </beans>

电脑培训,计算机培训,平面设计培训,网页设计培训,美工培训,Web培训,Web前端开发培训

2.在jsp页面的上传是一个按钮,然后按钮调用弹窗,在弹窗里面进行上传

2.1这个是js代码,数据表格是在页面刷新后自动执行的,把文件上传放在一个弹窗里面是因为,如果单独一个按钮上传,他会提示说没有选择图片,而且这个弹窗里面还有2个隐藏的Input来接收前台传进来的2个id,把值赋值给隐藏Input框,然后再拿出来传到后台,这样就可以获取到uuid,进行文件上传所需要的主键了(PS:传uuid一直显示unfind的话,要记得在uuid两边再加上双引号并要用转义符,因为有可能uuid自动生成里面会有特殊字符所以要再加上双引号)

电脑培训,计算机培训,平面设计培训,网页设计培训,美工培训,Web培训,Web前端开发培训

$(function(){
    $('#sjb_div').datagrid({   
        title:"文件管理",
        url:'../../UploadController/cakan.do',    
        pagination:true,
        toolbar:"#gongjulan",
        columns:[[    
                  {field:'up_uuid',title:'文件编号',align:'center'},    
                  {field:'us_name',title:'用户名',align:'center'},    
                  {field:'us_phone',title:'联系方式',align:'center'},    
                  {field:'up_name',title:'文件名',align:'center'},    
                  {field:'wjus_srid',title:'用户id',align:'center'},    
                  {field:'up_type',title:'文件分类',align:'center',formatter: function(value,row,index){
                      if (value==0){
                          return "加工文件";
                      } else if(value==1){
                          return "订单文件";
                      }
                  }
                  },    
                  {field:"up_shangchuan",title:'文件',align:'center',formatter: function(value,row,index){
                      if (row.up_name==null||row.up_name==""){
                          return "<a onclick='wjshangchuan(\""+row.wjus_srid+"\",\""+row.up_uuid+"\")' class='easyui-linkbutton' data-options='iconCls:'icon-add' style='color:blue'  ><u>上传</u></a>";
                      } else {
                          return "<a onclick='wjshangchuan(\""+row.wjus_srid+"\",\""+row.up_uuid+"\")' class='easyui-linkbutton' data-options='iconCls:'icon-add' style='color:blue'  ><u>替换</u></a>" +
                          "&nbsp;&nbsp; <a href='../../FileController/download.do?fileName="+row.up_name+"' onclick=wjxiazai(\""+row.up_name+"\")>下载</a>";                      }
                  }
                  },
                  ]]
    }); 
});

电脑培训,计算机培训,平面设计培训,网页设计培训,美工培训,Web培训,Web前端开发培训

2.2点击上传后调用的方法(把获取到的2个主键分别存到2个隐藏的input里面)把弹窗打开:

电脑培训,计算机培训,平面设计培训,网页设计培训,美工培训,Web培训,Web前端开发培训

//文件上传按钮static-----------------------------------------------------------------------------
function wjshangchuan(uuid,up_ud){
    document.getElementById("us_yingchang_input").value=uuid;//把值赋给隐藏input
    document.getElementById("up_yingchang_input").value=up_ud;//把值赋给隐藏input
    $('#wjshangchuan_div').dialog('open');    
}
//文件上传按钮end-----------------------------------------------------------------------------

电脑培训,计算机培训,平面设计培训,网页设计培训,美工培训,Web培训,Web前端开发培训

2.3这个是弹窗(弹窗里面有2个隐藏input框用来接收值)

电脑培训,计算机培训,平面设计培训,网页设计培训,美工培训,Web培训,Web前端开发培训

<!-- 文件下载start--------------------------------------------------- -->
    <div id="wjshangchuan_div" class="easyui-dialog" title="上传"
        style="width:400px;height:200px; top:150px"
        data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true">
        <form id="upload_form" method="post" enctype="multipart/form-data">
            <input typx="text" id="us_yingchang_input" type="hidden"> <input
                typx="text" id="up_yingchang_input" type="hidden"> <input
                type="file" name="uploadfile" /> <input type="button"
                onclick="wj_shangchuan()" value="上传" />
        </form>
    </div>
    <!-- 文件下载end--------------------------------------------------- -->

 

电脑培训,计算机培训,平面设计培训,网页设计培训,美工培训,Web培训,Web前端开发培训

2.4这个是上传文件的js(先拿到隐藏input框里面的值,然后把跟路径一起传到后台去)

电脑培训,计算机培训,平面设计培训,网页设计培训,美工培训,Web培训,Web前端开发培训

//文件上传static-----------------------------------------------------------------------------
function wj_shangchuan(){
    var zhi= $("#us_yingchang_input").val();//获取隐藏input值
    var up_uuid= $("#up_yingchang_input").val();//获取隐藏input值
    var attname;
    var path;
    var formData = new FormData($("#upload_form")[0]);
    $.ajax({
        url: '../../FileController/uploadFileCustmer.do?type=0&&uuid='+zhi+'&&up_uuid='+up_uuid,
        type: 'POST',
        data: formData,
        async: false,
        contentType: false,
        processData: false,
        dataType: "json",
        success: function (returndata) {
            path = returndata.path;
            attname = returndata.oname;
            if(path==null||attname==null){
                $.messager.alert('警告','请选择要上传的文件'); 
            }else{
                $.messager.alert('提示','附件上传成功!!!'); 
            }
            $('#sjb_div').datagrid('reload');  //重载数据表
            $("#wjshangchuan_div").dialog('close');//关闭弹窗
        },
        error: function (returndata) {

        }
    });
}
//文件上传end-----------------------------------------------------------------------------

电脑培训,计算机培训,平面设计培训,网页设计培训,美工培训,Web培训,Web前端开发培训

2.5图片展示:

电脑培训,计算机培训,平面设计培训,网页设计培训,美工培训,Web培训,Web前端开发培训

 

 电脑培训,计算机培训,平面设计培训,网页设计培训,美工培训,Web培训,Web前端开发培训

2.6弹窗里面的<form>表单要注意,文件上传的name,要和等等接下来的Controoler里面的 multipartResolver 对象的名字要一样,不然会找不到我们选择的文件,这个要注意

电脑培训,计算机培训,平面设计培训,网页设计培训,美工培训,Web培训,Web前端开发培训

<!-- 文件下载start--------------------------------------------------- -->

<form id="upload_form" method="post" enctype="multipart/form-data">
   <input type="file" name="uploadfile" />
   <input type="button" onclick="wj_shangchuan()" value="上传" />
        </form>
    </div>
    <!-- 文件下载end--------------------------------------------------- -->

 

电脑培训,计算机培训,平面设计培训,网页设计培训,美工培训,Web培训,Web前端开发培训

2.7:补2.6的图

电脑培训,计算机培训,平面设计培训,网页设计培训,美工培训,Web培训,Web前端开发培训

3.FileController

电脑培训,计算机培训,平面设计培训,网页设计培训,美工培训,Web培训,Web前端开发培训

package com.bypx.controller;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;import javax.activation.FileTypeMap;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.commons.io.FileUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.multipart.commons.CommonsMultipartFile;import com.bypx.page.UploadPage;import com.bypx.page.UserPage;import com.bypx.util.DateEasy;import com.bypx.util.DeleteFileUtil;import com.bypx.util.EnvironmentUtil;import com.bypx.util.JsonUtil;import com.bypx.util.KeyGenerate;import com.bypx.util.StringUtil;

@Controller
@RequestMapping("FileController")public class FileController {
    @Autowired    private JdbcTemplate jdbcTemplate;
    @RequestMapping("uploadFileCustmer")
    @ResponseBody    public String uploadFileCustmer(HttpServletRequest request,MultipartFile uploadfile, HttpSession session,UserPage page,UploadPage up_page) throws IOException {        // 1获得上传的文件内容
        byte[] bytes = uploadfile.getBytes();
        System.out.println(bytes+"..................47");        // 2获得upload的绝对路径
        String path =EnvironmentUtil.getInstance().getAppPath()+"upload/";
        String path2="upload/";
        System.out.println(path+"-----------------45-----path");
        System.out.println(uploadfile.getOriginalFilename()+"-------49------");        // 3在服务器的upload目录下创建File对象

        if (uploadfile.getOriginalFilename() != "" && uploadfile.getOriginalFilename() != null) {
            String time = new DateEasy().toYYYYMMDDHHMMSS();// 加随机数,防止重复
            System.out.println(time+"----------54-------");
            String extname="";            if(uploadfile.getOriginalFilename().indexOf(".")!=-1){
                extname=uploadfile.getOriginalFilename();
            }
            String oname = time+"_" +extname;  // 上传文件的原始名字
            System.out.println(oname+"..........60..........");
            File file = new File(path

http://www.cnblogs.com/likeji/p/7182732.html