如何在struts2 spring中将uploadedFile转换为文件而不给出路径

问题描述 投票:0回答:1

我需要使用

ActionUploadFileInterceptor
。我实现了
UploadedFilesAware
并需要将
UploadedFile
转换为
File
。我为
convertUploadedFileToFile
编写了一个方法。

有没有更好的方法在 Struts 中将

uploadFile
转换为
File

正确吗

convertUploadedFileToFile()
uploadedFile.getContent()
是什么类型? 这是我的代码:

@ConversationController
public class SampleAction extends BaseActionSupport implements UploadedFilesAware {

    private List<UploadedFile> uploadedFiles;
    public String upload() {
        if (uploadedFiles != null && !uploadedFiles.isEmpty()) {
            for (UploadedFile file : uploadedFiles) {
                String fileName = file.getOriginalName(); // Get the file name
                LOG.debug("uploading file for OwnToOther Batch {}", fileName);
                String ext = FilenameUtils.getExtension(fileName);
                if (ext.equalsIgnoreCase("xlsx") || ext.equalsIgnoreCase("xls")) {
                    processExcelFile(file);
                } else {
                    processCSVFile(file);
                }
            }
        }
        setGridModel(transferVOList.getFundTransferList());

        return SUCCESS;
    }

    private void processExcelFile(UploadedFile uploadedFile) {

        List<List<String>> dataEntriesList;
        List<FundTransferVO> transVOList;
        try {
            File file = convertUploadedFileToFile(uploadedFile);
            dataEntriesList = excelReader.read(file, COLUMN_NUMBER);
            transVOList = excelReader.populateBeans(dataEntriesList);
            // ...

        } catch (IOException ex) {
            LOG.error("Error in reading the uploaded excel file: ", ex);
        } catch (InvalidFormatException ex) {
            LOG.error("File format error while reading the uploaded excel file:", ex);
        }
    }

    private File convertUploadedFileToFile(UploadedFile uploadedFile) throws IOException {
        File file = new File(uploadedFile.getOriginalName());
        Object contentObject = uploadedFile.getContent();
        if (contentObject instanceof InputStream) {
            try (InputStream inputStream = (InputStream) contentObject;
                    FileOutputStream outputStream = new FileOutputStream(file)) {
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
            }
        } else if (contentObject instanceof byte[]) {
            byte[] content = (byte[]) contentObject;
            try (FileOutputStream outputStream = new FileOutputStream(file)) {
                outputStream.write(content);
            }
        } else if (contentObject instanceof String) {
            String content = (String) contentObject;
            try (FileOutputStream outputStream = new FileOutputStream(file)) {
                outputStream.write(content.getBytes());
            }
        } else if (contentObject instanceof File) {
            File contentFile = (File) contentObject;
            Files.copy(contentFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
        } else {
            throw new IOException("Unsupported content type: " + contentObject.getClass().getName());
        }

        return file;
    }
    @Override
    public void withUploadedFiles(List<UploadedFile> uploadedFiles) {
        this.uploadedFiles = uploadedFiles;
    }
}

有没有更好的方法在 Struts 中将

uploadFile
转换为
File

java file-upload struts2 java-io struts2-interceptors
1个回答
0
投票

如果您需要使用

actionFileUpload
拦截器,那么您应该阅读 Action 文件上传拦截器

基于

MultiPartRequestWrapper
的拦截器,它会自动应用于包含文件的任何请求。如果一个action实现了
org.apache.struts2.action.UploadedFilesAware
接口,拦截器会通过回调方法
withUploadedFiles(List<UploadedFile>)
传递上传文件的信息和内容。

请参阅示例页面。

如果您使用

application.properties
中的默认配置:

struts.multipart.parser=jakarta

那么您不需要将

UploadedFile::getContent
转换为
File
。如果您不使用自定义解析器,则直接转换它。仅
StrutsUploadedFile
实现了
UploadedFile
并且返回
File
类型

File file = (File) uploadedFile.getContent();
© www.soinside.com 2019 - 2024. All rights reserved.