0

我在这里有一个愚蠢的问题,我正在使用 vaadin 实现上传按钮,我希望用户只上传压缩文件(.zip,.rar ..),我进行了搜索,但我没有找到有用的东西:所以我试图这样做,我知道这不是一个好的解决方案,因为用户已经上传了选定的文件:

@Override
public OutputStream receiveUpload(String filename, String mimeType) {
   // Create upload stream
        FileOutputStream fos = null; // Stream to write to
        String fileName ;
        String userHome = System.getProperty( "user.home" );
        try {
            // Open the file for writing.
            file = new File(userHome+"/kopiMap/runtime/uploads/report/" + filename);
            fileName= file.getName();
                //Here i will get file extension
            fos = new FileOutputStream(file);
        } catch (final java.io.FileNotFoundException e) {
            Notification.show(
                    "Could not open file<br/>", e.getMessage(),
                    Notification.TYPE_ERROR_MESSAGE);
            return null;
        }
        return fos; // Return the output stream to write to
    }

那么在上传之前怎么做

4

2 回答 2

2

you can check the mimeType and if it is application/zip

@Override
public OutputStream receiveUpload(String filename, String mimeType) {
   // Create upload stream
if(mimeType.equals("application/zip"))
//Here you can restrict
于 2013-06-26T11:22:03.160 回答
1

您可以添加它,它会起作用(全部由 HTML 5 完成,现在大多数浏览器支持都接受属性) - 这是 .csv 文件的示例:

upload.setButtonCaption("Import");
JavaScript.getCurrent().execute("document.getElementsByClassName('gwt-FileUpload')[0].setAttribute('accept', '.csv')");
于 2016-04-25T11:55:40.037 回答