最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實現(xiàn)文件上傳下載功能

 更新時間:2021年08月26日 10:20:10   作者:wh456413  
這篇文章主要介紹了java實現(xiàn)文件上傳下載功能,上傳單個或多個文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java實現(xiàn)文件上傳下載的具體代碼,供大家參考,具體內(nèi)容如下

1.上傳單個文件

Controller控制層

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("testup")
public class UploadController {
    private static Logger LG= LoggerFactory.getLogger(UploadController.class);
    /**
     * 9.①單個文件上傳
     * @param file
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value="/upload",method=RequestMethod.POST,consumes="multipart/form-data")
    public String uploadFile(@RequestParam MultipartFile file,RedirectAttributes redirectAttributes){
        if(file.isEmpty()){
            redirectAttributes.addFlashAttribute("message", "Plse select file");
            return "redirect:/test/index";
        }
        try {
            String fileName=file.getOriginalFilename();
            /*上傳文件存儲位置*/
            String destFileName="D:\\whupload"+File.separator+fileName;
            File destFile=new File(destFileName);
            file.transferTo(destFile);
            //文件上傳成功顯示
            //redirectAttributes.addAttribute("message","upload file success.");
            redirectAttributes.addFlashAttribute("message", "upload file success.");
        } catch (Exception e) {
            //文件上傳失敗顯示
            redirectAttributes.addFlashAttribute("message", "upload file fail");
            LG.debug(e.getMessage());
        }
        return "redirect:/test/index";
    }

}

前端頁面源碼

<p>上傳文件,使用multipart/form-data類型</p>
  <form action="/testup/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="file">
        <button type="submit">上傳</button>
</form>

2.上傳多個文件

Controller控制層

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("testup")
public class UploadController {
    private static Logger LG= LoggerFactory.getLogger(UploadController.class);
  

    /**
     * 9.②多個文件上傳
     */
    @RequestMapping(value="/uploadBatchFile",method=RequestMethod.POST,consumes="multipart/form-data")
    public String uploadBatchFile(@RequestParam MultipartFile[] files,RedirectAttributes redirectAttributes){
        boolean isEmpty=true;
        try {
            for (MultipartFile multipartFile : files) {
                if(multipartFile.isEmpty()){
                    continue;
                }
                String fileName=multipartFile.getOriginalFilename();
                String destFileName="D:\\whupload"+File.separator+fileName;
                File destFile=new File(destFileName);
                multipartFile.transferTo(destFile);
                isEmpty=false;
            }
            //int i=1/0;
            //localhost:8086/test/index?message=upload file success
            //redirectAttributes.addAttribute("message","upload file success.");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            redirectAttributes.addFlashAttribute("message", "upload file fail");
            LG.debug(e.getMessage());
            return "redirect:/test/index";
        }
        if(isEmpty){
            redirectAttributes.addFlashAttribute("message", "Plse select file");
        }else{
            redirectAttributes.addFlashAttribute("message", "upload file success.");
        }
        return "redirect:/test/index";
    }

    
}

前端頁面源碼

<form action="/testup/uploadBatchFile" method="post" enctype="multipart/form-data">
        <input type="file" name="files">
        <input type="file" name="files">
        <button type="submit">上傳</button>
</form>

3.下載文件

Controller控制器

import java.io.File;
import java.net.MalformedURLException;
import java.nio.file.Paths;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
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;

@Controller
@RequestMapping("testup")
public class UploadController {
    private static Logger LG= LoggerFactory.getLogger(UploadController.class);
    

    /**
     * 10.下載文件
     */
    @RequestMapping("/download")
    @ResponseBody
    public ResponseEntity<Resource> downloadFile(@RequestParam String fileName){
        try {
            Resource resource=new UrlResource(
                    //拼接下載的文件的原路徑
                    Paths.get("D:/whupload"+File.separator+fileName).toUri());
            if(resource.exists() && resource.isReadable()){
                return ResponseEntity.ok()
                        .header(HttpHeaders.CONTENT_TYPE, "application/octet-stream")
                        .header(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename=\""+
                                resource.getFilename()+"\"").body(resource);

            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            LG.debug(e.getMessage());
        }
        return null;
    }
}

前端頁面源碼

<p>下載文件,這里設(shè)置默認(rèn)下載文件為Demo.txt,fileName是下載文件名</p>
<a href="/testup/download?fileName=Demo.txt" rel="external nofollow" >download file</a>

運行效果

最后,需要注意的是,文件上傳有默認(rèn)的大小限制
在配置文件中加入,即可消除限制

spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • MyBatis實現(xiàn)動態(tài)SQL的實現(xiàn)方法

    MyBatis實現(xiàn)動態(tài)SQL的實現(xiàn)方法

    這篇文章主要介紹了MyBatis實現(xiàn)動態(tài)SQL的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Java編程中快速排序算法的實現(xiàn)及相關(guān)算法優(yōu)化

    Java編程中快速排序算法的實現(xiàn)及相關(guān)算法優(yōu)化

    這篇文章主要介紹了Java編程中快速排序算法的實現(xiàn)及相關(guān)算法優(yōu)化,快速排序算法的最差時間復(fù)雜度為(n^2),最優(yōu)時間復(fù)雜度為(n\log n),存在優(yōu)化的空間,需要的朋友可以參考下
    2016-05-05
  • Spring Boot高效數(shù)據(jù)聚合之道深入講解

    Spring Boot高效數(shù)據(jù)聚合之道深入講解

    這篇文章主要給大家介紹了關(guān)于Spring Boot高效數(shù)據(jù)聚合之道的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • 自定義SpringBoot的白標(biāo)錯誤頁面的操作方法

    自定義SpringBoot的白標(biāo)錯誤頁面的操作方法

    Spring Boot的白標(biāo)錯誤頁面是在應(yīng)用程序出現(xiàn)錯誤時(如404或500 HTTP狀態(tài)碼)自動生成的默認(rèn)錯誤頁面,下面小編給大家分享如何自定義SpringBoot的白標(biāo)錯誤頁面,感興趣的朋友跟隨小編一起看看吧
    2024-06-06
  • 獲取Spring當(dāng)前配置的兩種方式

    獲取Spring當(dāng)前配置的兩種方式

    這篇文章主要給大家介紹了獲取Spring當(dāng)前配置的,兩種方式文中通過代碼示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-01-01
  • SpringBoot日志配置簡單介紹

    SpringBoot日志配置簡單介紹

    這篇文章主要介紹了SpringBoot日志配置,需要的朋友可以參考下
    2017-09-09
  • 使用Java實現(xiàn)類似Comet風(fēng)格的web app

    使用Java實現(xiàn)類似Comet風(fēng)格的web app

    這篇文章主要介紹了使用Java實現(xiàn)類似Comet風(fēng)格的web app的方法,包括客戶端的響應(yīng)和XML解析等功能,需要的朋友可以參考下
    2015-11-11
  • 如何使用IDEA查看java文件編譯后的字節(jié)碼內(nèi)容

    如何使用IDEA查看java文件編譯后的字節(jié)碼內(nèi)容

    這篇文章主要介紹了如何使用IDEA查看java文件編譯后的字節(jié)碼內(nèi)容,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • springboot自定義starter實現(xiàn)過程圖解

    springboot自定義starter實現(xiàn)過程圖解

    這篇文章主要介紹了springboot自定義starter實現(xiàn)過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • Java通過匿名類來實現(xiàn)回調(diào)函數(shù)實例總結(jié)

    Java通過匿名類來實現(xiàn)回調(diào)函數(shù)實例總結(jié)

    這篇文章主要介紹了Java通過匿名類來實現(xiàn)回調(diào)函數(shù)的例子,回調(diào)函數(shù)就是一種函數(shù)簽名(若干個輸入?yún)?shù)、一個輸出參數(shù))的規(guī)范,java雖不存在函數(shù)聲明,但是java可以用接口來強制規(guī)范。具體操作步驟大家可查看下文的詳細(xì)講解,感興趣的小伙伴們可以參考一下。
    2017-08-08

最新評論

田阳县| 于田县| 尚志市| 庆元县| 常宁市| 灵川县| 岐山县| 仁寿县| 铁力市| 荔浦县| 青海省| 安吉县| 岚皋县| 江都市| 广南县| 开原市| 柘城县| 韩城市| 稷山县| 永城市| 卢氏县| 乐业县| 祥云县| 华阴市| 阿坝县| 旬阳县| 西畴县| 大足县| 仁寿县| 宜昌市| 莒南县| 平原县| 长子县| 微博| 循化| 大港区| 克拉玛依市| 上林县| 兴和县| 板桥市| 会同县|