欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

使用MultipartResolver怎么實現(xiàn)一個文件上傳功能

這篇文章將為大家詳細(xì)講解有關(guān)使用MultipartResolver怎么實現(xiàn)一個文件上傳功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:空間域名、網(wǎng)絡(luò)空間、營銷軟件、網(wǎng)站建設(shè)、龍華網(wǎng)站維護、網(wǎng)站推廣。

springMVC默認(rèn)的解析器里面是沒有加入對文件上傳的解析的,,使用springmvc對文件上傳的解析器來處理文件上傳的時需要用springmvc提供的MultipartResolver的申明,又因為CommonsMultipartResolver實現(xiàn)了MultipartResolver接口,所以我們可以在springmvc配置文件中這樣配置:

 <bean id="multipartResolver" 
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    <property name="defaultEncoding" value="utf-8" /> 
    <property name="maxUploadSize" value="10485760000" /> 
    <property name="maxInMemorySize" value="40960" /> 
  </bean>

 首先引入文件上傳所需要的包,commons-logging-*.jar commons-io-*.jar  commons-fileupload-*.jar

新建一個JSP頁面.

<%@ 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> 
  <%--<form action="user/fileUpload" method="post" enctype="multipart/form-data">--%> 
  <form action="user/fileUpload" method="post" enctype="multipart/form-data"> 
    <input type="file" name="fileUpload" /> 
    <input type="submit" value="上傳" /> 
  </form> 
</body> 
</html>

springmvc上傳文件的形式有很多,這里我介紹兩種.

第一種,看Controller

package gd.hz.springmvc.controller; 
 
import java.io.File; 
import java.io.IOException; 
 
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.commons.CommonsMultipartFile; 
import org.springframework.web.servlet.ModelAndView; 
 
@Controller("userController") 
@RequestMapping("user") 
public class UserController { 
 
  // 處理文件上傳一 
  @RequestMapping(value = "fileUpload", method = RequestMethod.POST) 
  public ModelAndView fileUpload( 
      @RequestParam("fileUpload") CommonsMultipartFile file) { 
    // 獲取文件類型 
    System.out.println(file.getContentType()); 
    // 獲取文件大小 
    System.out.println(file.getSize()); 
    // 獲取文件名稱 
    System.out.println(file.getOriginalFilename()); 
 
    // 判斷文件是否存在 
    if (!file.isEmpty()) { 
      String path = "D:/" + file.getOriginalFilename(); 
      File localFile = new File(path); 
      try { 
        file.transferTo(localFile); 
      } catch (IllegalStateException e) { 
        e.printStackTrace(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
    } 
    return new ModelAndView("dataSuccess"); 
  } 
}

類CommonsMultipartFile為我們提供了許多對文件處理的方法.例如文件大小,上傳文件名稱,文件類型,具體用法可以查看spring的文檔.transferTo就是將文件輸出到指定地方. 

文件上傳的第二種方法,這種方法比較常用:

package gd.hz.springmvc.controller; 
 
import java.io.File; 
import java.io.IOException; 
import java.util.Iterator; 
 
import javax.servlet.http.HttpServletRequest; 
 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.multipart.MultipartFile; 
import org.springframework.web.multipart.MultipartHttpServletRequest; 
import org.springframework.web.multipart.commons.CommonsMultipartResolver; 
 
@Controller("userController") 
@RequestMapping("user") 
public class UserController { 
 
  // 處理文件上傳二 
  @RequestMapping(value = "fileUpload2", method = RequestMethod.POST) 
  public String fileUpload2(HttpServletRequest request) 
      throws IllegalStateException, IOException { 
    // 設(shè)置上下方文 
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( 
        request.getSession().getServletContext()); 
 
    // 檢查form是否有enctype="multipart/form-data" 
    if (multipartResolver.isMultipart(request)) { 
      MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; 
 
      Iterator<String> iter = multiRequest.getFileNames(); 
      while (iter.hasNext()) { 
 
        // 由CommonsMultipartFile繼承而來,擁有上面的方法. 
        MultipartFile file = multiRequest.getFile(iter.next()); 
        if (file != null) { 
          String fileName = "demoUpload" + file.getOriginalFilename(); 
          String path = "D:/" + fileName; 
 
          File localFile = new File(path); 
          file.transferTo(localFile); 
        } 
 
      } 
    } 
    return "dataSuccess"; 
  } 
}

關(guān)于使用MultipartResolver怎么實現(xiàn)一個文件上傳功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

文章題目:使用MultipartResolver怎么實現(xiàn)一個文件上傳功能
網(wǎng)頁地址:http://www.chinadenli.net/article48/ieoiep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站域名注冊面包屑導(dǎo)航網(wǎng)站內(nèi)鏈微信小程序網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名