Java Krajee设计的引导文件输入JQuery插件(SyntaxError:输入意外结束)

Java Krajee设计的引导文件输入JQuery插件(SyntaxError:输入意外结束),java,jquery,ajax,twitter-bootstrap,servlets,Java,Jquery,Ajax,Twitter Bootstrap,Servlets,我有个问题。我用它来上传文件,好吧,文件成功上传到服务器上,但在jsp页面上显示消息“SyntaxError:输入意外结束”,尽管文件已成功上传到服务器上。 我的servlet,我认为这里有问题(可能是JSON) public类UploadImageCommand实现ActionCommand{ 枚举类型{ 图像(“/upload/IMAGES”、“.jpg”、“.bmp”、“.gif”、“.png”、“.jpeg”), 视频(“/upload/VIDEOS”、“.avi”、“.mpeg”、“

我有个问题。我用它来上传文件,好吧,文件成功上传到服务器上,但在jsp页面上显示消息“SyntaxError:输入意外结束”,尽管文件已成功上传到服务器上。

我的servlet,我认为这里有问题(可能是JSON)

public类UploadImageCommand实现ActionCommand{
枚举类型{
图像(“/upload/IMAGES”、“.jpg”、“.bmp”、“.gif”、“.png”、“.jpeg”),
视频(“/upload/VIDEOS”、“.avi”、“.mpeg”、“.mpg”、“.mp4”、“.mov”、“.mkv”、“.flv”),
音乐(“/upload/music”、“.mp3”、“.wav”);
私有字符串路径;
私有字符串[]格式;
类型(字符串路径、字符串…格式){
this.path=path;
this.formats=格式;
}
公共字符串[]getFormats(){
返回格式;
}
公共字符串getPath(){
返回路径;
}
}
私有静态字符串parseFileFormat(字符串文件名){
fileName=fileName.toLowerCase();
int dotPosition=fileName.lastIndexOf(“.”);
字符串格式=fileName.substring(点位置,fileName.length());
返回格式;
}
私有类型getType(字符串文件名){
字符串格式=parseFileFormat(文件名);
Type[]values=Type.values();
对于(int i=0;i
}

如中所述:

您必须从服务器发送一个有效的JSON响应,否则将无法上载 这个过程将失败。即使您没有遇到任何错误,您也必须 至少从服务器发送一个空的JSON对象{}


我已经解决了我的问题。我在servlet的末尾添加了以下代码:response.setContentType(“application/json”);JSONObject JSONObject=新的JSONObject();jsonObject.put(“,”);打印(jsonObject);关闭();但是我不明白空JSON是如何解决我的问题的。那么,回答你自己的问题,这样“未回答”的列表就可以了。另外,使用'twitter bootstrap'标记,而不是'bootstrap'是的,您可以使用
void
返回空对象。
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");
}