Java 上传文件时如何将输入文本值传递给servlet?
我有两个文件,一个是jsp文件,第二个是servlet文件 当我在jsp文件中包含输入文本字段以将文本传递到servlet文件时,当运行发出错误消息的程序时,为什么会发生这种情况 index.jsp代码:Java 上传文件时如何将输入文本值传递给servlet?,java,jsp,servlets,Java,Jsp,Servlets,我有两个文件,一个是jsp文件,第二个是servlet文件 当我在jsp文件中包含输入文本字段以将文本传递到servlet文件时,当运行发出错误消息的程序时,为什么会发生这种情况 index.jsp代码: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>File Uplo
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Upload</title>
</head>
<body>
<center>
<h1>File Upload</h1>
<form method="post" action="UploadServlet"
enctype="multipart/form-data">
<table border="1">
<tr><td><h3>Input Text at Top of image :</h3> </td><td><input type="text" name="name1" /></td></tr>
<tr><td><h3>Input Text at Bottom of image :</h3> </td><td><input type="text" name="name2" /></td></tr>
</table><br>
Select file to upload: <input type="file" name="file" size="60" /><br />
<br /> <input type="submit" value="Upload" />
</form>
</center>
</body>
</html>
文件上传
文件上传
在图像顶部输入文本:
在图像底部输入文本:
选择要上载的文件:
UploadServlet.java servlet文件:
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet("/UploadServlet")
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
maxFileSize=1024*1024*10, // 10MB
maxRequestSize=1024*1024*50) // 50MB
public class UploadServlet extends HttpServlet
{
/**
* Name of the directory where uploaded files will be saved, relative to
* the web application directory.
*/
private static final String SAVE_DIR = "uploadFiles";
/**
* handles file upload
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// gets absolute path of the web application
String appPath = request.getServletContext().getRealPath("");
// constructs path of the directory to save uploaded file
String savePath = appPath + File.separator + SAVE_DIR+ File.separator +"kala";
// creates the save directory if it does not exists
File fileSaveDir = new File(savePath);
//File dir = new File("E:/Users/al/tmp/000/111/222");
if(fileSaveDir.exists()){}
else
{// create multiple directories at one time
boolean successful = fileSaveDir.mkdirs();
if (successful)
{
// created the directories successfully
System.out.println("directories were created successfully");
}
else
{
// something failed trying to create the directories
System.out.println("failed trying to create the directories");
}
}
// create multiple directories at one time
//boolean successful = fileSaveDir.mkdirs();
System.out.println(savePath);
//savePath="C:\\Users\\SANJAY GUPTA\\Downloads\\eclipse";
for (Part part : request.getParts())
{
String fileName = extractFileName(part);
// refines the fileName in case it is an absolute path
fileName = new File(fileName).getName();
part.write(savePath + File.separator + fileName);
}
String text1 = "";
String text2 = "";
text1 = request.getParameter("name1");
text2 = request.getParameter("name2");
request.setAttribute("message", "Upload has been done successfully!"+"<br>"+savePath);
getServletContext().getRequestDispatcher("/message.jsp").forward(
request, response);
}
/**
* Extracts file name from HTTP header content-disposition
*/
private String extractFileName(Part part)
{
String contentDisp = part.getHeader("content-disposition");
String[] items = contentDisp.split(";");
for (String s : items)
{
if (s.trim().startsWith("filename"))
{
return s.substring(s.indexOf("=") + 2, s.length()-1);
}
}
return "";
}
//UploadServlet u = new UploadServlet();
}
导入java.io.File;
导入java.io.IOException;
导入javax.servlet.ServletException;
导入javax.servlet.annotation.MultipartConfig;
导入javax.servlet.annotation.WebServlet;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入javax.servlet.http.Part;
@WebServlet(“/UploadServlet”)
@MultipartConfig(fileSizeThreshold=1024*1024*2,//2MB
maxFileSize=1024*1024*10,//10MB
maxRequestSize=1024*1024*50)//50MB
公共类UploadServlet扩展了HttpServlet
{
/**
*将保存上载文件的目录的名称,相对于
*web应用程序目录。
*/
私有静态最终字符串SAVE_DIR=“uploadFiles”;
/**
*处理文件上传
*/
受保护的void doPost(HttpServletRequest请求,
HttpServletResponse响应)引发ServletException,IOException
{
//获取web应用程序的绝对路径
字符串appPath=request.getServletContext().getRealPath(“”);
//构造目录的路径以保存上载的文件
字符串savePath=appPath+File.separator+SAVE_DIR+File.separator+kala;
//如果不存在保存目录,则创建该目录
File fileSaveDir=新文件(保存路径);
//File dir=新文件(“E:/Users/al/tmp/000/111/222”);
如果(fileSaveDir.exists()){}
其他的
{//一次创建多个目录
boolean successful=fileSaveDir.mkdirs();
如果(成功)
{
//已成功创建目录
System.out.println(“目录创建成功”);
}
其他的
{
//尝试创建目录时失败
System.out.println(“尝试创建目录失败”);
}
}
//一次创建多个目录
//boolean successful=fileSaveDir.mkdirs();
System.out.println(保存路径);
//savePath=“C:\\Users\\SANJAY GUPTA\\Downloads\\eclipse”;
for(部分:request.getParts())
{
字符串文件名=提取文件名(部分);
//如果文件名是绝对路径,则细化文件名
fileName=新文件(fileName).getName();
写入(savePath+File.separator+fileName);
}
字符串text1=“”;
字符串text2=“”;
text1=request.getParameter(“name1”);
text2=request.getParameter(“name2”);
request.setAttribute(“消息”,“上传成功!”+“
”+savePath);
getServletContext().getRequestDispatcher(“/message.jsp”).forward(
请求、答复);
}
/**
*从HTTP头内容处置中提取文件名
*/
私有字符串提取文件名(部分)
{
字符串contentDisp=part.getHeader(“内容处置”);
String[]items=contentDisp.split(“;”);
用于(字符串s:项)
{
如果(s.trim().startsWith(“文件名”))
{
返回s.substring(s.indexOf(“=”)+2,s.length()-1);
}
}
返回“”;
}
//UploadServlet u=新的UploadServlet();
}
如何将从jsp传递到servlet的值转换为text1和text2字符串变量?
错误被附加为图像
在图像顶部输入文本:
在图像底部输入文本:
错误为文本:
HTTP状态500-java.io.FileNotFoundException:C:\Users\Dell\workspaceluna1.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\UploadServlet31\uploadFiles\kala(访问被拒绝)
类型异常报告
消息java.io.FileNotFoundException:C:\Users\Dell\workspaceluna1\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\UploadServlet31\uploadFiles\kala(访问被拒绝)
说明服务器遇到内部错误,无法满足此请求。
例外
java.io.IOException:java.io.FileNotFoundException:C:\Users\Dell\workspaceluna1\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\UploadServlet31\uploadFiles\kala(访问被拒绝)
net.codejava.servlet.UploadServlet.doPost(UploadServlet.java:70)
javaservlet.http.HttpServlet.service(HttpServlet.java:650)
javaservlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
根本原因
java.io.FileNotFoundException:C:\Users\Dell\workspaceluna1\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\UploadServlet31\uploadFiles\kala(访问被拒绝)
java.io.FileOutputStream.open(本机方法)
FileOutputStream.java.io.FileOutputStream。(FileOutputStream.java:221)
FileOutputStream.java.io.FileOutputStream。(FileOutputStream.java:171)
org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.write(DiskFileItem.java:372)
net.codejava.servlet.UploadServlet.doPost(UploadServlet.java:70)
javaservlet.http.HttpServlet.service(HttpServlet.java:650)
javaservlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
注意f
<table border="1">
<tr><td><h3>Input Text at Top of image :</h3> </td><td><input type="text" name="name1" /></td></tr>
<tr><td><h3>Input Text at Bottom of image :</h3> </td><td><input type="text" name="name2" /></td></tr>
</table>
type Exception report
message java.io.FileNotFoundException: C:\Users\Dell\workspaceluna1\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\UploadServlet31\uploadFiles\kala (Access is denied)
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.io.IOException: java.io.FileNotFoundException: C:\Users\Dell\workspaceluna1\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\UploadServlet31\uploadFiles\kala (Access is denied)
net.codejava.servlet.UploadServlet.doPost(UploadServlet.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.io.FileNotFoundException: C:\Users\Dell\workspaceluna1\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\UploadServlet31\uploadFiles\kala (Access is denied)
java.io.FileOutputStream.open(Native Method)
java.io.FileOutputStream.<init>(FileOutputStream.java:221)
java.io.FileOutputStream.<init>(FileOutputStream.java:171)
org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.write(DiskFileItem.java:372)
net.codejava.servlet.UploadServlet.doPost(UploadServlet.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.73 logs.