控制上传的文件类型(只能上传特定的扩展名)

时间:2013-06-26 11:04:46

标签: java file-upload vaadin

我有一个愚蠢的问题,我正在用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
    }

那么在上传之前如何做到

2 个答案:

答案 0 :(得分:2)

您可以检查mimeType以及它是否为application / zip

@Override
public OutputStream receiveUpload(String filename, String mimeType) {
   // Create upload stream
if(mimeType.equals("application/zip"))
//Here you can restrict

答案 1 :(得分:1)

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

upload.setButtonCaption("Import");
JavaScript.getCurrent().execute("document.getElementsByClassName('gwt-FileUpload')[0].setAttribute('accept', '.csv')");
相关问题