微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 由Krajee设计的Bootstrap文件输入JQuery插件(SyntaxError:意外的输入结束)

我有个问题.我使用这个http://plugins.krajee.com/file-input/demo进行文件上传,好吧,文件成功上传到服务器上,但是在jsp页面显示消息“SyntaxError:意外结束输入.”,虽然文件已成功上传到服务器上.

我的servlet,我觉得这里有问题(也许是JSON).

public class UploadImageCommand implements ActionCommand {

enum Type {

    IMAGES("/upload/images", ".jpg", ".bmp", ".gif", ".png", ".jpeg"),
    VIDEOS("/upload/videos", ".avi", ".mpeg", ".mpg", ".mp4", ".mov", ".mkv", ".flv"),
    MUSICS("/upload/musics", ".mp3", ".wav");

    private String path;
    private String[] formats;

    Type(String path, String... format) {
        this.path = path;
        this.formats = format;
    }

    public String[] getFormats() {
        return formats;
    }

    public String getPath() {
        return path;
    }
}

private static String parseFileFormat(String fileName) {
    fileName = fileName.toLowerCase();
    int dotPosition = fileName.lastIndexOf(".");
    String format = fileName.substring(dotPosition, fileName.length());
    return format;
}

private Type getType(String fileName) {
    String format = parseFileFormat(fileName);
    Type[] values = Type.values();
    for (int i = 0; i < values.length; i++) {
        for (int j = 0; j < values[i].getFormats().length; j++) {
            if (values[i] == Type.IMAGES && values[i].getFormats()[j].equals(format)) {
                return Type.IMAGES;
            } else if (values[i] == Type.VIDEOS && values[i].getFormats()[j].equals(format)) {
                return Type.VIDEOS;
            } else if (values[i] == Type.MUSICS && values[i].getFormats()[j].equals(format)) {
                return Type.MUSICS;
            }
        }
    }
    return null;
}

@Override
public void execute(HttpServletRequest request, HttpServletResponse response)
        throws servletexception, IOException {

    ServletContext context = request.getSession().getServletContext();
    if (ServletFileUpload.isMultipartContent(request)) {
        try {
            String fileName = null;
            String filePath;
            Type type = null;
            List<FileItem> multiparts = new ServletFileUpload(
                    new diskFileItemFactory()).parseRequest(request);
            System.out.println("Multipart size: " + multiparts.size());
            for (FileItem item : multiparts) {
                if (item.getName() == null || item.getName() == "") {
                    continue;
                }
                System.out.println("Part : " + item.getName());
                if (!item.isFormField()) {
                    fileName = new File(item.getName()).getName();
                    type = getType(fileName);
                    filePath = context.getRealPath(type.path);
                    if (type != null) {
                        SecureRandom random = new SecureRandom();
                        fileName = new BigInteger(130, random).toString(32) +
                                parseFileFormat(fileName);
                        item.write(new File(filePath + File.separator + fileName));
                        System.out.println("File uploaded successfully");
                        // System.out.println("File path: " + context.getRealPath(type.path));
                    } else {
                        throw new IllegalStateException("Wrong file format!");
                    }
                }
            }
            // response.getWriter().print(json.toString());
        } catch (Exception e) {
            e.printstacktrace();
        }
    } else {
        System.out.println("Sorry this Servlet only handles file upload request");
    }
    response.setContentType("application/json");
} 

}

解决方法:

正如http://plugins.krajee.com/file-input#async-send中所注意到的:

You MUST send a valid JSON response from your server, else the upload
process will fail. Even if you do not encounter any error, you must at
least send an empty JSON object {} from your server.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐