如何从绝对文件路径制作CommonsMultipartFile? [英] How to make CommonsMultipartFile from absolute file path?

查看:2516
本文介绍了如何从绝对文件路径制作CommonsMultipartFile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序创建API。在基于GUI浏览器的应用程序中,文件通过表单提交上载。所以我只需要 CommonsMultipartFile file = request.getFile(myfile)。但是,API将以字符串形式提供文件的绝对路径,而不是上载文件。我的应用程序将可以访问此绝对路径。

I'm creating an API for my application. In the GUI browser based application the file is uploaded via a form submission. So I simply do CommonsMultipartFile file = request.getFile(myfile). However, the API will provide an absolute path to the file as a string rather than uploading the file. My application will have access to this absolute path.

因此我不必更改应用程序的基础方法(接受通用接口 MultiPartFile 出于API目的,我想从这个绝对路径读取文件并创建一个 CommonsMultipartFile 对象,该对象可以传递给我已经用于基于GUI浏览器的方法应用程序。

So that I don't have to change the underlying methods of my application (which accept the common interface MultiPartFile For API purposes, I would like to read the file from this absolute path and create a CommonsMultipartFile object which can be passed around to the methods that I am already using for GUI browser based application.

如何执行此操作? CommonsMultipartFile 的构造函数接受 FileItem

How can I do this? Constructor to CommonsMultipartFile accepts a FileItem

推荐答案

这是特定于API的代码。即不是通常的文件上传代码。

This is API-specific code. i.e. not the usual file upload code.

通常的步骤是:


  1. 构建FileItemFactory

  2. 构造ServletFileUpload,将其传递给工厂

  3. 调用ServletFileUpload.parseRequest(请求)

这回答用独立于servlet的逻辑替换2& 3 - 它避免使用ServletFileUpload(特定于servlet)及其祖先FileUpload(以便用绝对路径名控制文件位置)。注意:(3)通常检查HTTP请求参数以确定传递给FileItemFactory.createItem的较低级别参数 - 这些参数是手动提供的,然后仅用作信息元数据。替换为2& 3:

This answer replaces 2 & 3 with logic independent of servlets - it avoids using ServletFileUpload (servlet-specific) and its ancestor FileUpload (so as to control the file location with an absolute path name). Note: (3) usually examines HTTP request parameters to determine lower-level parameters that are passed to FileItemFactory.createItem - these parameters are instead provided manually, and then only used as informational metadata. Replacement for 2 & 3:


  • 构造FileItem(通过FileItemFactory.createItem - 需要手动提供较低级别的参数,通常通过ServletFileUpload.upload()确定)

  • 使用绝对路径写入特定文件

  • 通过MultipartFile上传文件

下面提供的请求代码。最后它调用公共代码 - 与Servlet上传共享。

Requested code provided below. At the end it invokes common code - shared with Servlet upload.

// Initialise Apache Commons FileItemFactory for API use only
FileItemFactory fif = new DiskFileItemFactory(sizeThreshold, repositoryBaseDirFile);

// Create Apache Commons FileItem & write file at fullFilePathString into it
FileItem fi = fif.createItem(fieldName, contentType, isFormField, fileName);
fi.write(new java.io.File(new java.net.URI(fullFilePathString));

// Convert FileItem to Spring wrapper: CommonsMultipartFile
org.springframework.web.multipart.MultipartFile mf = new CommonsMultipartFile(fi);

// From here, reuse the same code as the servlet upload.  Operate only upon  
// Spring MultipartFile, but not ServletFileUpload, FileItemFactory etc...






参数:


Parameters:


  • fullFilePathString :将上传文件的绝对路径(作为字符串)

  • fieldName :表单上的字段名称

  • fullFilePathString: absolute path (as String) where file will be uploaded
  • fieldName: name of field on the form

(因为避免使用ServletFileUpload& FileUpload,以下仅成为元数据字段,并且不用于控制处理)

(Because ServletFileUpload & FileUpload are avoided, the following become metadata fields only, and are not used to control processing)


  • sizeThreshhold :内存大小阈值(以字节为单位) (通常较小的文件仅使用内存上传,较大的文件上传通过磁盘aded - 但是这个逻辑总是通过磁盘上传文件。默认值= DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD。

  • repositoryBaseDireFile 通常文件上传'temp'目录(作为文件类型),但是这个逻辑使用绝对路径上传文件

  • contentType :表单上字段的内容类型(MIME类型)(如果不是多部分表单字段,则为null)

  • isFormField :如果是普通表单字段,则为true,否则为false,如果是多部分字段。

  • fileName :文件名 - 通常通过表单/客户端指定。

  • sizeThreshhold: memory size threshold in bytes (usually files smaller are uploaded using memory only and files larger are uploaded via disk - but this logic has files always uploaded via disk). Default = DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD.
  • repositoryBaseDireFile: usually the file upload 'temp' directory (as a File type), but this logic uses an absolute path to upload file
  • contentType: content type (MIME type) of field on the form (null if not multi-part form field)
  • isFormField: if plain form field, 'true', else false if multi-part field.
  • fileName: the name of the file - usually specified via form / client.

这篇关于如何从绝对文件路径制作CommonsMultipartFile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆