我使用以下代码使用html和java servlet上传文件。
但是我无法打印位置或检索上传文件的路径的值。
请帮我解决一下我是如何打印出来的。我想在我的servlet文件中打印例如:d:/new/filename
。
提前致谢。以下是我的代码段:
**index.html**
<html>
<body>
<form action="go" method="post" enctype="multipart/form-data">
Select File:<input type="file" name="fname"/><br/>
<input type="submit" value="upload"/>
</form>
</body>
</html>
**UploadServlet.java**
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import com.oreilly.servlet.MultipartRequest;
public class UploadServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
MultipartRequest m=new MultipartRequest(request,"d:/new");
out.print("successfully uploaded");
}
}
***web.xml***
<web-app>
<servlet>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/go</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:1)
我会试试
out.print("successfully uploaded in" + m.getFilesystemName("fname"));