将图像从本地窗口上传到Java中的远程位置文件夹



我能够在本地上传图像,但无法远程上传(即另一台计算机)。

@webservlet("/uploadServ")

公共类上载Servlet扩展了httpservlet {

private static final String UPLOAD_DIR1 = "\\ip-address\C$\upload\";
public UploadServlet() {
    super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (!isMultipart) {
    } else {
        try {
            List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory())
                    .parseRequest(new ServletRequestContext(request));
            for (FileItem item : multiparts) {
                if (!item.isFormField()) {
                    String name = new File(item.getName()).getName();
                    item.write(new File(UPLOAD_DIR1 + name));
                }
            }
            // File uploaded successfully
            System.out.println("File uploaded successfully");
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("File uploaded failed..");
        }
    }}
    request.getRequestDispatcher("response.jsp").forward(request, response);
}

为了创建相对于Web内容root("/")的正确文件夹URL,我使用了以下代码

final String uploadDir =  request.getRealPath("/ip-address/C$/upload"); 

然后只创建一个新文件对象

File file = new File( uploadDir, fileName);

请注意,getRealPath()现在已弃用,但仍应起作用。根据此答案(推荐将上传文件保存在Servlet应用程序中的推荐方法)有更好的方法来指定上传文件夹,但我没有尝试过任何这些方法。

相关内容

  • 没有找到相关文章

最新更新