【问题标题】:Bootstrap File Input JQuery Plugin designed by Krajee (SyntaxError: Unexpected end of input)Krajee设计的Bootstrap File Input JQuery Plugin (SyntaxError: Unexpected end of input)
【发布时间】:2015-09-05 12:00:55
【问题描述】:

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

我的 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");
} 

}

【问题讨论】:

  • 我已经解决了我的问题。我在 servlet 的末尾添加了这段代码: response.setContentType("application/json"); JSONObject jsonObject = 新 JSONObject(); jsonObject.put("", ""); pw.print(jsonObject); pw.close();但我不明白空 JSON 是如何解决我的问题的
  • 然后,回答您自己的问题,这样“未回答”的列表就可以了。另外,使用“twitter-bootstrap”标签,而不是“bootstrap”

标签: java jquery ajax twitter-bootstrap servlets


【解决方案1】:

如http://plugins.krajee.com/file-input#async-send 中所述:

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

【讨论】:

  • 是的,你可以用void返回来返回空对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-24
  • 2018-02-19
  • 2021-09-21
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
  • 1970-01-01
相关资源
最近更新 更多