使用java和dropzone.js上传文件不起作用

时间:2015-04-17 09:42:44

标签: java javascript jsp file-upload dropzone.js

我开始使用dropzone.js,我想上传一个文件,不幸的是这个工作不起作用。我提到我从一个有效的项目中获得了灵感。 所以这是我的控制器类:

@Controller
public class FileUploadController {

    private FileUploadService uploadService = (FileUploadService) SpringContext.CONTEXT
            .getContext().getBean(FileUploadService.class);

    @RequestMapping(value = { "/file" })
    public String home() {

        return "fileUploader";
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public @ResponseBody List<UploadedFile> upload(
            MultipartHttpServletRequest request, HttpServletResponse response)
            throws IOException {

        System.out.println("Enter in upload.....");
        // Getting uploaded files from the request object
        Map<String, MultipartFile> fileMap = request.getFileMap();

        // Maintain a list to send back the files info. to the client side
        List<UploadedFile> uploadedFiles = new ArrayList<UploadedFile>();

        // Iterate through the map
        for (MultipartFile multipartFile : fileMap.values()) {

            // Save the file to local disk
            saveFileToLocalDisk(multipartFile);

            UploadedFile fileInfo = getUploadedFileInfo(multipartFile);

            // Save the file info to database
            fileInfo = saveFileToDatabase(fileInfo);

            // adding the file info to the list
            uploadedFiles.add(fileInfo);
        }

        return uploadedFiles;
    }

    @RequestMapping(value = { "/list" })
    public String listBooks(Map<String, Object> map) {

        map.put("fileList", uploadService.listFiles());

        return "/listFiles";
    }

    @RequestMapping(value = "/get/{fileId}", method = RequestMethod.GET)
    public void getFile(HttpServletResponse response, @PathVariable int fileId) {

        UploadedFile dataFile = uploadService.getFile(fileId);

        File file = new File(dataFile.getLocation(), dataFile.getName());

        try {
            response.setContentType(dataFile.getType());
            response.setHeader("Content-disposition", "attachment; filename=\""
                    + dataFile.getName() + "\"");

            FileCopyUtils.copy(FileUtils.readFileToByteArray(file),
                    response.getOutputStream());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void saveFileToLocalDisk(MultipartFile multipartFile)
            throws IOException, FileNotFoundException {

        String outputFileName = getOutputFilename(multipartFile);

        FileCopyUtils.copy(multipartFile.getBytes(), new FileOutputStream(
                outputFileName));
    }

    private UploadedFile saveFileToDatabase(UploadedFile uploadedFile) {

        return uploadService.saveFile(uploadedFile);

    }

    private String getOutputFilename(MultipartFile multipartFile) {

        return getDestinationLocation() + multipartFile.getOriginalFilename();
    }

    private UploadedFile getUploadedFileInfo(MultipartFile multipartFile)
            throws IOException {

        UploadedFile fileInfo = new UploadedFile();
        fileInfo.setName(multipartFile.getOriginalFilename());
        fileInfo.setSize(multipartFile.getSize());
        fileInfo.setType(multipartFile.getContentType());
        fileInfo.setLocation(getDestinationLocation());
        fileInfo.setActuality("previous");

        return fileInfo;
    }

    private String getDestinationLocation() {
        return "D:/uploaded-files/";
    }
}` 

在上传方法中我输入一条消息(System.out.println(“Enter in upload .....”))以检查是否输入此方法但不输入。

'fileUploader'jsp是:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" name="viewport"
    content="width=device-width, initial-scale=1">
<title>Spring MVC + Dropzone.js Example</title>

<link rel="stylesheet" type="text/css"
    href='<c:url value="/web-resources/libs/bootstrap-3.1.1/css/bootstrap.min.css"/>'>
<link rel="stylesheet" type="text/css"
    href='<c:url value="/web-resources/libs/bootstrap-dialog/css/bootstrap-dialog.min.css"/>'>
<link rel="stylesheet" type="text/css"
    href='<c:url value="/web-resources/css/style.css"/>'>

</head>
<body>
    <div class="container">
        <div class="panel panel-default">
            <div class="panel-heading text-center">
                <h3>Spring MVC + Dropzone.js Example</h3>
            </div>
            <div class="panel-body">
                <div>
                    <form id="dropzone-form" class="dropzone"
                        enctype="multipart/form-data">

                        <div
                            class="dz-default dz-message file-dropzone text-center well col-sm-12">

                            <span class="glyphicon glyphicon-paperclip"></span> <span>
                                To attach files, drag and drop here</span><br> <span>OR</span><br>
                            <span>Just Click</span>
                        </div>

                        <!-- this is were the previews should be shown. -->
                        <div class="dropzone-previews"></div>
                    </form>
                    <hr>
                    <button id="upload-button" class="btn btn-primary">
                        <span class="glyphicon glyphicon-upload"></span> Upload
                    </button>
                    <a class="btn btn-primary pull-right" href="list">
                        <span class="glyphicon glyphicon-eye-open"></span> View All Uploads
                    </a>
                </div>
            </div>
        </div>
    </div>

    <script type="text/javascript"
        src='<c:url value="/web-resources/libs/jquery/jquery-2.1.1.js"/>'></script>
    <script type="text/javascript"
        src='<c:url value="/web-resources/libs/bootstrap-3.1.1/js/bootstrap.js"/>'></script>
    <script type="text/javascript"
        src='<c:url value="/web-resources/libs/bootstrap-dialog/js/bootstrap-dialog.min.js"/>'></script>
    <script type="text/javascript"
        src='<c:url value="/web-resources/libs/dropzone.js"/>'></script>
    <script type="text/javascript"
        src='<c:url value="/web-resources/js/app.js"/>'></script>
</body>
</html>`

现在问题是我选择文件后按“上传”按钮时出现的错误。错误是:

<html><head><title>Apache Tomcat/7.0.57 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 405 - Request method 'POST' not supported</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Request method 'POST' not supported</u></p><p><b>description</b> <u>The specified HTTP method is not allowed for the requested resource.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.57</h3></body></html>

在浏览器中出现上述错误,在控制台中没有出现任何内容。

我的servlet cod如下:

    <?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Handles HTTP GET requests for /web-resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources/ directory -->

    <!-- Defines a resolver implementation bean. It gets applied to all requests 
        handled by that DispatcherServlet -->
    <beans:bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

    <beans:bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
        <beans:property name="useTrailingSlashMatch" value="true" />
    </beans:bean>

    <resources mapping="/web-resources/**" location="/resources/" />


    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.license.web.*" />


</beans:beans>

提前多多感谢!

0 个答案:

没有答案