どのように、ダウンロードファイルをアップロードするには、ブラウザからのwebappsディレクトリー

最初のJSPページのコード:

<form method="post" action="<%= request.getContextPath()%>/user/file/upload"
      enctype="multipart/form-data">
    <input type="file" name="upFile">
    <input type="submit" name="" value="上传">
</form>
<br/>
<a href="<%= request.getContextPath() %>/user/file/download?file3=a.txt">文件下载A</a>

:これは、Springフレームワークを使用してMavenプロジェクトで
アップロードダウンロード:webappsの下のダウンロードファイル

1、ジャーパッケージを依存しています:

第一个jar包用于文件上传处理,第二个jar包用于上传的文件输出到硬盘
	<dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.4</version>
    </dependency>

    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>

2、Spring.xmlファイル構成ファイルプロセッサフ​​ァイル:

 <!--上传的文件解析器-->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="maxInMemorySize" value="10485760"></property>
    </bean>

3、ファイルのアップロード:

3つのコード、以下のすべての列、することができ、任意に1つがあります。

コントローラレベルコード第一実施形態:
  	@RequestMapping("/file/upload")
    public void upload(MultipartFile upFile,HttpServletRequest request)  {
    
        //上传文件改名:
        
        //得到上传文件安置地址的绝对路径
        String uploadDir = request.getServletContext().getRealPath("download");
        String newFileName = UUID.randomUUID().toString();
        
        //用MultipartFile的对象方法得到文件名
        String oldFileName = upFile.getOriginalFilename();
        int index = oldFileName.lastIndexOf(".");
        String extName = oldFileName.substring(index);
        newFileName = newFileName + extName;

       //上传文件:
        FileOutputStream outputStream = null;
        try {
        //建立输出到硬盘指定目录的输出流
            outputStream = new FileOutputStream(new File(uploadDir, newFileName));
        //用commons-io   jar包里的IOUtils工具输出
            IOUtils.copy(upFile.getInputStream(), outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
第二の方法をアップロードします。

直接転写方式MultipartFileオブジェクトによって。

	@RequestMapping("/file/upload")
    public void upload(MultipartFile upFile,HttpServletRequest request)  {
        //上传文件改名:
        String uploadDir = request.getServletContext().getRealPath("download");
        String newFileName = UUID.randomUUID().toString();
        String oldFileName = upFile.getOriginalFilename();
        int index = oldFileName.lastIndexOf(".");
        String extName = oldFileName.substring(index);
        newFileName = newFileName + extName;
        
      //上传文件:
      try {
            upFile.transferTo(new File(uploadDir, newFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }*/
コードをアップロードする第三の方法:
 	@RequestMapping("/file/upload")
    public void upload(MultipartFile upFile,HttpServletRequest request)  {
       InputStream inputStream =null;
        try {
            inputStream = upFile.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //上传文件改名:
        String uploadDir = request.getServletContext().getRealPath("download");
        String newFileName = UUID.randomUUID().toString();
        String oldFileName = upFile.getOriginalFilename();
        int index = oldFileName.lastIndexOf(".");
        String extName = oldFileName.substring(index);
        newFileName = newFileName + extName;

		//上传文件:
        //用输出流把文件输出到download文件夹
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(new File(uploadDir, newFileName));
            byte[] data = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(data)) != -1) {
                outputStream.write(data,0,len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
}

4、ファイルをダウンロードします。

 	@RequestMapping("/file/download")
    public void download(HttpServletResponse response, HttpServletRequest request) {

        //取得要下载的文件名
        String fileName = request.getParameter("file3");

        // 约定所有下载文件在服务器上的目录位置
        String download = request.getServletContext().getRealPath("download");
        File file = new File(download, fileName);

        // 处理下载
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        InputStream input = null;
        OutputStream out = null;
        try {
            input = new FileInputStream(file);
            out = response.getOutputStream();
            byte[] data = new byte[1024];
            int len = 0;
            while ((len = input.read()) != -1) {
                out.write(data,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
公開された14元の記事 ウォンの賞賛0 ビュー319

おすすめ

転載: blog.csdn.net/qq_38205881/article/details/104199426