简体   繁体   English

将文件存储到服务器 - java

[英]Storing a file to server - java

I have searched for hours and tried many examples.我已经搜索了几个小时并尝试了很多例子。 None of which give me a result that remotely works.这些都没有给我一个远程工作的结果。 I am using eclipse scout and want to simply convert my binary resource from selecting a file to be stored in a directory.我正在使用 eclipse scout 并想简单地将我的二进制资源从选择要存储在目录中的文件转换。 Here I have a button that when clicked it prompts you to select a file to upload (they will only be PDFs for now) and the result I get is a binary resource list.在这里,我有一个按钮,单击它会提示您选择要上传的文件(它们现在只是 PDF),我得到的结果是一个二进制资源列表。 I have no idea how I can write that as an input stream.我不知道如何将其写为输入流。 And if using input and output streams isn't the correct option I have not found a solution that allows me to chose a file and store it to C://FolderName/FileNameIChoose.如果使用输入和输出流不是正确的选择,我还没有找到允许我选择文件并将其存储到 C://FolderName/FileNameIChoose 的解决方案。

@Order(1750)
        public class UploadReceiptButton extends AbstractButton {
            @Override
            protected String getConfiguredLabel() {
                return TEXTS.get("UploadReceipt");
            }

            @Override
            protected void execClickAction() {
                FileChooser fc = new FileChooser(true);
                List<BinaryResource> data = fc.startChooser();
                System.out.println(data);
                //This is where the data from that file should be stored on my C drive as a file
            }
        }

The result of the data binary resource when selecting test.pdf is:选择test.pdf时数据二进制资源的结果为:

 [BinaryResource, content: 260502 bytes, filename: test.pdf, contentType: application/pdf, fingerprint: 1281876091]]

If anyone can point me in the right direction that would be extremally helpful to me and Im sure to a lot of others.如果有人能指出我正确的方向,那将对我非常有帮助,而且我肯定对很多其他人都有帮助。

The BinaryResource object simply holds the content of the uploaded file in memory as byte[] array, along with some additional metadata such as the filename or the MIME content type. BinaryResource对象只是将上传文件的内容保存在内存中作为byte[]数组,以及一些额外的元数据,例如文件名或 MIME 内容类型。 You can store or process it any way you like.您可以以任何您喜欢的方式存储或处理它。

To write it to a file, you can either use a library (eg Apache Commons) or the standard Java file API.要将其写入文件,您可以使用库(例如 Apache Commons)或标准 Java 文件 API。 Scout also provides some helpful methods in their IOUtility to work with files. Scout 还在其IOUtility中提供了一些有用的方法来处理文件。

FileChooser fc = new FileChooser(true);
List<BinaryResource> data = fc.startChooser();
data.forEach(br => {
  String filename = "C:/MyFolder/" + FileUtility.toValidFilename(br.getFilename());
  
  // Scout
  IOUtility.writeContent(filename, br.getContent());
  
  // Java File API (requires at least Java 7)
  try {
    Files.write(Paths.get(filename), br.getContent());
  }
  catch (IOException e) {
    throw BEANS.get(DefaultRuntimeExceptionTranslator.class).translate(e);
  }
});

Please be aware, that by doing this you are creating a file on the machine where the UI server is running !请注意,这样做是在运行 UI 服务器的机器上创建一个文件! This may be fine if your application is only used by a single user or is running on your local machine.如果您的应用程序仅由单个用户使用或在您的本地计算机上运行,​​这可能很好。 Otherwise, you should store it in some kind of database.否则,您应该将其存储在某种数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM