简体   繁体   English

如何将文件从Java servlet上传到Tomcat / webapps中Web服务器上的某个位置

[英]How do I upload a file from a Java servlet to a location on a web server within Tomcat/webapps

How do I upload a file from a Java servlet to a location on a web server within Tomcat/webapps. 如何将文件从Java servlet上传到Tomcat / webapps中Web服务器上的某个位置。

I am using Commons upload. 我正在使用Commons上传。 I have a location such as myserver:8080/myapp/mylocation where I want to put the files that are uploaded. 我有一个位置,例如myserver:8080/myapp/mylocation ,我要在其中放置上载的文件。

I tried using getServletContext().getRealPath("/"); 我尝试使用getServletContext().getRealPath("/"); to find where I am and then appended that with mylocation but I get a nullpointer exception. 查找我的位置,然后将其附加到mylocation上,但出现空指针异常。

I know I sound vague, it's because I am confused about the big picture, what are the generals steps I need to perform to make this work. 我知道我听起来有些含糊,这是因为我对总体情况感到困惑,为完成这项工作我需要执行哪些一般步骤。 Any code or links to code would be much appreciated. 任何代码或代码链接将不胜感激。

You may need to check permissions. 您可能需要检查权限。 Make sure that you have write permissions on the directory in which you plan to store the file. 确保对计划存储文件的目录具有写权限。 Generally you may want to store the file in WEB-INF/mylocation. 通常,您可能希望将文件存储在WEB-INF / mylocation中。 Then getServletContext().getRealPath("/WEB-INF/mylocation") becomes the location of where you are going to save the file. 然后,getServletContext()。getRealPath(“ / WEB-INF / mylocation”)成为您要保存文件的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将war文件部署到webapps后,Java项目未在tomcat管理器的tomcat服务器上运行? - Java project is not running on tomcat server from tomcat manager after deploying war file to webapps? 如何从tomcat服务器Web应用程序位置下载zip文件 - How to download zip file from tomcat server web app location Java:如何从servlet上传文件到WebDAV服务器? - Java: How to upload a file to a WebDAV server from a servlet? 在部署简单的Web应用程序时,java文件(即servlet)不在服务器上运行(Tomcat) - While deploying a simple web application, the java file (i.e servlet) doesn't run on server (Tomcat) 如何从 Java servlet 中抛出 404 错误? - How do I throw a 404 error from within a java servlet? 你如何在tomcat / java webapps中配置HttpOnly cookie? - How do you configure HttpOnly cookies in tomcat / java webapps? 从Java Servlet Web应用程序将文件上传到我的保管箱 - Upload a file to my dropbox from a java servlet web-application 如何使用javac编译的类文件在tomcat 7中构造Java servlet? - How do I construct a java servlet in tomcat 7 using a javac compiled class file? 如何关闭Java Apache服务器HttpRequestHandler.handle()中的套接字以拒绝上传? - how do i close the socket from within the java apache server HttpRequestHandler.handle() to reject an upload? 从servlet中写入文件(Deployer:Tomcat) - Writing to file from within a servlet(Deployer:Tomcat)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM