3

私はこれを実装したい:ファイルチェックファイル拡張子が.zipでない場合、アップロード中にアップロードが中断され、ユーザーにメッセージが表示されるので、これを書きました:

// Implement both receiver that saves upload in a file and
// listener for successful upload
class ReportUploader implements Receiver, SucceededListener {
    public File file;

    public OutputStream receiveUpload(String filename, String mimeType) {
        // Create upload stream
        FileOutputStream fos = null; // Stream to write to
        String userHome = System.getProperty("user.home");

        String ext = filename.substring(filename.lastIndexOf('.'));
        System.out.println("+++++++++++>" + ext);  
        if (!ext.equals(".zip")) {
            Notification.show("Could not upload file", Notification.TYPE_ERROR_MESSAGE);
            return null;
        }
        try {
            file = new File(userHome + "/uploads/report/" + filename);
            fos = new FileOutputStream(file);
        } catch (final java.io.FileNotFoundException e) {
            Notification.show("Could not open file", e.getMessage(), Notification.TYPE_ERROR_MESSAGE);
            return null;
        }
        return fos; 
    }

    public void uploadSucceeded(SucceededEvent event) {
        String filename;
        String basepath;
        String folderName;
        String uploadDir;

        filename = event.getFilename();
        basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
        uploadDir = Utilities.getProperty("uploadDir", basepath + "/WEB-INF/properties/config.properties"); 
        file = new File(uploadDir + filename);
        folderName = filename.substring(0, filename.length() - 5);

        Utilities.unZipFolder(uploadDir + filename, uploadDir + folderName);
        addFilename(folderName);

        try {
            Utilities.deleteCompressedReport(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Notification.show("upload finished", Notification.TYPE_HUMANIZED_MESSAGE); 
    }

    private void addFilename(String sFilename) {
        reportDirList.add(sFilename);
        selectDir.addItem(sFilename);
    }
}  

拡張子が .zip でない場合、次のエラーが発生します。

com.vaadin.server.UploadException: Upload failed
at com.vaadin.server.communication.FileUploadHandler.streamToReceiver(FileUploadHandler.java:581)
at com.vaadin.server.communication.FileUploadHandler.handleFileUploadValidationAndData(FileUploadHandler.java:419)
at com.vaadin.server.communication.FileUploadHandler.doHandleSimpleMultipartFileUpload(FileUploadHandler.java:382)
at com.vaadin.server.communication.FileUploadHandler.handleRequest(FileUploadHandler.java:267)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1329)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:236)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: com.vaadin.server.NoOutputStreamException
at com.vaadin.server.communication.FileUploadHandler.streamToReceiver(FileUploadHandler.java:518)
... 22 more

では、アップロードを中断してメッセージを表示する方法は?

4

2 に答える 2

1

ソリューション :

com.vaadin.ui.Upload.StartedListener. を実装し、拡張子を持つファイル名を取得します。拡張機能が有効でない場合は、アップロード コンポーネントでメソッド interruptUpload() を呼び出します。

于 2013-08-22T13:32:24.997 に答える