SpringMVC如何實(shí)現(xiàn)多個(gè)文件上傳及上傳后立即顯示圖片功能

這篇文章主要為大家展示了“SpringMVC如何實(shí)現(xiàn)多個(gè)文件上傳及上傳后立即顯示圖片功能”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“SpringMVC如何實(shí)現(xiàn)多個(gè)文件上傳及上傳后立即顯示圖片功能”這篇文章吧。

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),硚口企業(yè)網(wǎng)站建設(shè),硚口品牌網(wǎng)站建設(shè),網(wǎng)站定制,硚口網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,硚口網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

多文件上傳就是改良一個(gè)方法把MultipartFile類換成CommonsMultipartFile類,因?yàn)樯蟼鞫鄠€(gè)文件用數(shù)組方式的話MultipartFile類不能初始化,它不支持?jǐn)?shù)組

package com.meng.upload;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
@Controller
public class upload {
  @RequestMapping(value="/upload",method=RequestMethod.POST)
  public String upload(@RequestParam("file1") CommonsMultipartFile[] file1, HttpServletRequest request) {
    for (CommonsMultipartFile commonsMultipartFile : file1) {
      try {
        File file = new File(request.getServletContext().getRealPath("upload"),
            System.currentTimeMillis() + "_"
                + commonsMultipartFile.getOriginalFilename());
        System.out.println(file.getPath()+"");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(commonsMultipartFile.getBytes());
        fileOutputStream.close();
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    return "ok";
  }
}

index.jsp頁(yè)面上傳立即顯示圖片

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
 <head>
  <title>My JSP 'index.jsp' starting page</title>
  <script type="text/javascript">
  //判斷瀏覽器是否支持FileReader接口
  if (typeof FileReader == 'undefined') {
    alert("<h2>當(dāng)前瀏覽器不支持FileReader接口</h2>");
  } 
  //選擇圖片,馬上預(yù)覽
  function xmTanUploadImg(obj) {
    var file = obj.files[0];
    var reader = new FileReader();
    reader.onload = function(e) {
      var img = document.getElementById("img1");
      img.src = e.target.result;
    }
    reader.readAsDataURL(file);
  }
  </script>
 </head>
 <body>
  <form action="upload.upload" method="post" enctype="multipart/form-data">
    file1::<input type="file" name="file1" accept=".jpg,.png" onchange="xmTanUploadImg(this)" />
    <input type="submit" />
    <img id="img1" />
  </form>
 </body>
</html>

以上是“SpringMVC如何實(shí)現(xiàn)多個(gè)文件上傳及上傳后立即顯示圖片功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文名稱:SpringMVC如何實(shí)現(xiàn)多個(gè)文件上傳及上傳后立即顯示圖片功能
URL標(biāo)題:http://bm7419.com/article16/pciodg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、小程序開(kāi)發(fā)外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站制作、面包屑導(dǎo)航

廣告

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

營(yíng)銷型網(wǎng)站建設(shè)