Google App Engine:405上传文件时不允许POST方法

时间:2017-03-27 15:32:11

标签: java servlets

当我尝试将文件上传到我的服务器时出现问题,出现405 Method POST is not allowed异常,此异常仅出现在生产中而非本地出现,

这是我的servlet处理表格后请求

public class FileUploadServlet extends HttpServlet {


@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS");
    resp.setHeader("Access-Control-Allow-Headers", "*");
    resp.setHeader("Access-Control-Allow-Origin", "*");
    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

    String uploadUrl = blobstoreService.createUploadUrl("/uploadCallback");
    uploadUrl = uploadUrl.substring(uploadUrl.indexOf("_") - 1);

    req.getRequestDispatcher(uploadUrl).forward(req, resp);
  }
}

处理来自' FileUploadServlet'

的请求的servlet
public class FileUploadedCallbackServlet extends HttpServlet {

private static BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

@Override
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
    Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
    String blobKey = blobs.get(FileUploadInput.FILE_URL).get(0).getKeyString(); // Name of field in form where file name was entered
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    out.print(blobKey);
}

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // TODO Auto-generated method stub

    doPost(req, resp);
  }
}

0 个答案:

没有答案