1. 程式人生 > >SpringMVC上傳文件後返回文件服務器地址路徑

SpringMVC上傳文件後返回文件服務器地址路徑

char tps null name inf html4 detect color rto

先寫一個表單:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上傳</title> </head> <body> <h2>文件上傳</h2> <form action="/fileUpload.jspx" enctype="multipart/form-data" method="post"> <table> <tr> <td>文件描述:</td> <td><input type="text"
name="description"></td> </tr> <tr> <td>請選擇文件:</td> <td><input type="file" name="uploadFile"></td> </tr> <tr> <td><input type="submit" value
="上傳"></td> </tr> </table> </form> </body> </html>

action層接收文件處理:

/**
     * 資源文件上傳,返回一個文件服務器地址
     * @author Libin
     * @date 2018年1月12日 上午10:28:08
     */
    @RequestMapping(value ="/fileUpload.jspx")
    public void uploadFileBackAddress(HttpServletRequest request, HttpServletResponse response) {
      try {
            MultipartFile multipartFile = null;
            if (request instanceof MultipartHttpServletRequest) {
                multipartFile = ((MultipartHttpServletRequest)request).getFile("uploadFile");
            }
            if (null != multipartFile) {
                /**
                 * 項目服務器地址路徑
                 */
                String projectServerPath = request.getScheme() + "://"+request.getServerName()+":" +
                                request.getServerPort() + request.getContextPath() + "/upload/";
                /**
                 * 上傳文件絕對路徑
                 */
                String path = request.getSession().getServletContext().getRealPath("/WEB-INF/upload/");
                /**
                 * 上傳文件名
                 */
                String fileName = makeFileName(multipartFile.getOriginalFilename());
                
                File file = new File(path + fileName);
                /**
                 * 判斷路徑是否存在,如果不存在就創建一個
                 */
                if (!file.getParentFile().exists()) { 
                    
                    file.getParentFile().mkdirs();
                }
                /**
                 * 創建文件
                 */
                multipartFile.transferTo(new File(path + File.separator + fileName));
                /**
                 * 返回服務器文件地址
                 */
                String serverFilePatn = projectServerPath + fileName;
               
            }
            
        }  catch (Exception e) {
            ajaxBackData = this.getAjaxBackDataExceptionStatus(e);
        }
       
    }
  
springMVC配置文件:
<?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:jee="http://www.springframework.org/schema/jee"
    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-4.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd"
    default-lazy-init="true">
    <context:annotation-config />
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"></property>
        <property name="maxUploadSize" value="10485760000"></property>
        <property name="maxInMemorySize" value="40960"></property>
    </bean>
    <mvc:annotation-driven></mvc:annotation-driven>
    <!-- 在web.xml配置detectAllViewResolvers為false不根據ViewResolver接口全掃描,只根據viewResolver標識查找DispatcherServlet的視圖解析器 -->
    <bean id="defaultViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <!-- 設置前綴,即視圖所在的路徑 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 設置後綴,即視圖的擴展名 -->
        <property name="suffix" value=".jsp" />
    </bean>
   
</beans>



參考文章:http://www.cnblogs.com/klslb/p/8286762.html

需要添加的jar包:commons-fileupload-1.2.1.jar commons-io.2.5.jar

SpringMVC上傳文件後返回文件服務器地址路徑