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

查看:84
本文介绍了如何从绝对文件路径制作 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(request)

这个答案取代了 2 &3 具有独立于 servlet 的逻辑 - 它避免使用 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...

<小时>

参数:

  • 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:以字节为单位的内存大小阈值(通常较小的文件仅使用内存上传,较大的文件通过磁盘上传 - 但此逻辑始终通过磁盘上传文件).默认值 = 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天全站免登陆