Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将一个jsp页面的图像和文本转换为另一个jsp页面_Javascript_Java_Html_Jsp_Servlets - Fatal编程技术网

Javascript 如何将一个jsp页面的图像和文本转换为另一个jsp页面

Javascript 如何将一个jsp页面的图像和文本转换为另一个jsp页面,javascript,java,html,jsp,servlets,Javascript,Java,Html,Jsp,Servlets,我有两个Jsp页面Form.Jsp和UploadFile.Jsp Form.jsp <%@ page language="java" %> <HTML> <FORM ENCTYPE="multipart/form-data" ACTION="UploadFile.jsp" METHOD=POST> <center> <table border="0" bgcolor=#ccFDDEE> <tr> <center&g

我有两个Jsp页面Form.Jsp和UploadFile.Jsp

Form.jsp

<%@ page language="java" %>
<HTML>
<FORM ENCTYPE="multipart/form-data" ACTION="UploadFile.jsp" METHOD=POST>
<center>
<table border="0" bgcolor=#ccFDDEE>


<tr>
<center><td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>


<tr><td colspan="2" align="center"> </td></tr>
<tr><td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>


<tr><td colspan="2" align="center"> </td></tr>
<tr><td colspan="2" align="center"> 
<input type="text" name="fn"></input><br>
<input type="text" name="ln"></input><br>
<input type="submit" value="Send File"> </td></tr>
</table>
 </center>

</FORM>
</HTML>
<%@ page import="java.io.*,java.sql.*,java.util.zip.*" %>
<%/* String firstname=request.getParameter("fname");
String lastname=request.getParameter("lname");
System.out.println("This is fname:"+firstname);
System.out.println("This is lname:"+lastname); */
String fname=request.getParameter("fn");
out.println("Welcome:"+fname);
String lname=request.getParameter("ln");
System.out.println("The value of lname is:"+lname);
String saveFile="";
String contentType = request.getContentType();
if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
System.out.println("I am while loop:"+totalBytesRead);
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File ff = new File("D:\\REAL\\RoseIndia\\images\\"+saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
System.out.println("THE FILE OUT PATH ARE:"+fileOut);
System.out.println("THE FF PATH ARE:"+ff.getPath());
fileOut.write(dataBytes, startPos, (endPos - startPos));
System.out.println("This is the SAVEFILE:::"+saveFile);
fileOut.flush();
System.out.println("111111111111111111111");
fileOut.close();
System.out.println("2222222222222222222");


%><% String name=request.getParameter("FNAME"); out.print("welcome "+name); %>  

System.out.println("The test value are::::"+name);
%><br><table border="2"><tr><td><b>You have successfully upload the file:</b>


<%out.println(ff.getPath());%></td></tr></table>
<FORM ENCTYPE="multipart/form-data" ACTION="DisplayImage" METHOD=POST>
<input type="submit" value="Send File">
</form>

<%
System.out.println("333333333333333");
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/realtime";
PreparedStatement ps = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");


ps = connection.prepareStatement("insert into file(image) values(?)");
ps.setString(1, ff.getPath());
/* ps.setString(2, firstname);
ps.setString(3, lastname); */
int s = ps.executeUpdate();
if(s>0){
System.out.println("Uploaded successfully !");

}
else{
System.out.println("Error!");
}
}
catch(Exception e){
    e.printStackTrace();
}
}
%>
我想发送图像(fb.png)和像fname和lname值这样的名称是UploadFile.jsp页面

在此之前,我尝试发送图像,但在尝试获取fname并通过文本框检索空值后,我成功地发送了该图像

Form.jsp

<%@ page language="java" %>
<HTML>
<FORM ENCTYPE="multipart/form-data" ACTION="UploadFile.jsp" METHOD=POST>
<center>
<table border="0" bgcolor=#ccFDDEE>


<tr>
<center><td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>


<tr><td colspan="2" align="center"> </td></tr>
<tr><td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>


<tr><td colspan="2" align="center"> </td></tr>
<tr><td colspan="2" align="center"> 
<input type="text" name="fn"></input><br>
<input type="text" name="ln"></input><br>
<input type="submit" value="Send File"> </td></tr>
</table>
 </center>

</FORM>
</HTML>
<%@ page import="java.io.*,java.sql.*,java.util.zip.*" %>
<%/* String firstname=request.getParameter("fname");
String lastname=request.getParameter("lname");
System.out.println("This is fname:"+firstname);
System.out.println("This is lname:"+lastname); */
String fname=request.getParameter("fn");
out.println("Welcome:"+fname);
String lname=request.getParameter("ln");
System.out.println("The value of lname is:"+lname);
String saveFile="";
String contentType = request.getContentType();
if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
System.out.println("I am while loop:"+totalBytesRead);
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File ff = new File("D:\\REAL\\RoseIndia\\images\\"+saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
System.out.println("THE FILE OUT PATH ARE:"+fileOut);
System.out.println("THE FF PATH ARE:"+ff.getPath());
fileOut.write(dataBytes, startPos, (endPos - startPos));
System.out.println("This is the SAVEFILE:::"+saveFile);
fileOut.flush();
System.out.println("111111111111111111111");
fileOut.close();
System.out.println("2222222222222222222");


%><% String name=request.getParameter("FNAME"); out.print("welcome "+name); %>  

System.out.println("The test value are::::"+name);
%><br><table border="2"><tr><td><b>You have successfully upload the file:</b>


<%out.println(ff.getPath());%></td></tr></table>
<FORM ENCTYPE="multipart/form-data" ACTION="DisplayImage" METHOD=POST>
<input type="submit" value="Send File">
</form>

<%
System.out.println("333333333333333");
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/realtime";
PreparedStatement ps = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");


ps = connection.prepareStatement("insert into file(image) values(?)");
ps.setString(1, ff.getPath());
/* ps.setString(2, firstname);
ps.setString(3, lastname); */
int s = ps.executeUpdate();
if(s>0){
System.out.println("Uploaded successfully !");

}
else{
System.out.println("Error!");
}
}
catch(Exception e){
    e.printStackTrace();
}
}
%>

上传文件
选择要上载的文件:


UploadFile.jsp

<%@ page language="java" %>
<HTML>
<FORM ENCTYPE="multipart/form-data" ACTION="UploadFile.jsp" METHOD=POST>
<center>
<table border="0" bgcolor=#ccFDDEE>


<tr>
<center><td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>


<tr><td colspan="2" align="center"> </td></tr>
<tr><td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>


<tr><td colspan="2" align="center"> </td></tr>
<tr><td colspan="2" align="center"> 
<input type="text" name="fn"></input><br>
<input type="text" name="ln"></input><br>
<input type="submit" value="Send File"> </td></tr>
</table>
 </center>

</FORM>
</HTML>
<%@ page import="java.io.*,java.sql.*,java.util.zip.*" %>
<%/* String firstname=request.getParameter("fname");
String lastname=request.getParameter("lname");
System.out.println("This is fname:"+firstname);
System.out.println("This is lname:"+lastname); */
String fname=request.getParameter("fn");
out.println("Welcome:"+fname);
String lname=request.getParameter("ln");
System.out.println("The value of lname is:"+lname);
String saveFile="";
String contentType = request.getContentType();
if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
System.out.println("I am while loop:"+totalBytesRead);
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File ff = new File("D:\\REAL\\RoseIndia\\images\\"+saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
System.out.println("THE FILE OUT PATH ARE:"+fileOut);
System.out.println("THE FF PATH ARE:"+ff.getPath());
fileOut.write(dataBytes, startPos, (endPos - startPos));
System.out.println("This is the SAVEFILE:::"+saveFile);
fileOut.flush();
System.out.println("111111111111111111111");
fileOut.close();
System.out.println("2222222222222222222");


%><% String name=request.getParameter("FNAME"); out.print("welcome "+name); %>  

System.out.println("The test value are::::"+name);
%><br><table border="2"><tr><td><b>You have successfully upload the file:</b>


<%out.println(ff.getPath());%></td></tr></table>
<FORM ENCTYPE="multipart/form-data" ACTION="DisplayImage" METHOD=POST>
<input type="submit" value="Send File">
</form>

<%
System.out.println("333333333333333");
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/realtime";
PreparedStatement ps = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");


ps = connection.prepareStatement("insert into file(image) values(?)");
ps.setString(1, ff.getPath());
/* ps.setString(2, firstname);
ps.setString(3, lastname); */
int s = ps.executeUpdate();
if(s>0){
System.out.println("Uploaded successfully !");

}
else{
System.out.println("Error!");
}
}
catch(Exception e){
    e.printStackTrace();
}
}
%>
在这个页面中,我将给定的图像保存到项目文件夹中,并获取图像的路径。使用此代码,我可以检索图像,但无法检索文本。。我想检索图像和文本

UploadFile.jsp

<%@ page language="java" %>
<HTML>
<FORM ENCTYPE="multipart/form-data" ACTION="UploadFile.jsp" METHOD=POST>
<center>
<table border="0" bgcolor=#ccFDDEE>


<tr>
<center><td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>


<tr><td colspan="2" align="center"> </td></tr>
<tr><td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>


<tr><td colspan="2" align="center"> </td></tr>
<tr><td colspan="2" align="center"> 
<input type="text" name="fn"></input><br>
<input type="text" name="ln"></input><br>
<input type="submit" value="Send File"> </td></tr>
</table>
 </center>

</FORM>
</HTML>
<%@ page import="java.io.*,java.sql.*,java.util.zip.*" %>
<%/* String firstname=request.getParameter("fname");
String lastname=request.getParameter("lname");
System.out.println("This is fname:"+firstname);
System.out.println("This is lname:"+lastname); */
String fname=request.getParameter("fn");
out.println("Welcome:"+fname);
String lname=request.getParameter("ln");
System.out.println("The value of lname is:"+lname);
String saveFile="";
String contentType = request.getContentType();
if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
System.out.println("I am while loop:"+totalBytesRead);
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File ff = new File("D:\\REAL\\RoseIndia\\images\\"+saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
System.out.println("THE FILE OUT PATH ARE:"+fileOut);
System.out.println("THE FF PATH ARE:"+ff.getPath());
fileOut.write(dataBytes, startPos, (endPos - startPos));
System.out.println("This is the SAVEFILE:::"+saveFile);
fileOut.flush();
System.out.println("111111111111111111111");
fileOut.close();
System.out.println("2222222222222222222");


%><% String name=request.getParameter("FNAME"); out.print("welcome "+name); %>  

System.out.println("The test value are::::"+name);
%><br><table border="2"><tr><td><b>You have successfully upload the file:</b>


<%out.println(ff.getPath());%></td></tr></table>
<FORM ENCTYPE="multipart/form-data" ACTION="DisplayImage" METHOD=POST>
<input type="submit" value="Send File">
</form>

<%
System.out.println("333333333333333");
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/realtime";
PreparedStatement ps = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");


ps = connection.prepareStatement("insert into file(image) values(?)");
ps.setString(1, ff.getPath());
/* ps.setString(2, firstname);
ps.setString(3, lastname); */
int s = ps.executeUpdate();
if(s>0){
System.out.println("Uploaded successfully !");

}
else{
System.out.println("Error!");
}
}
catch(Exception e){
    e.printStackTrace();
}
}
%>

= 0)){
DataInputStream in=新的DataInputStream(request.getInputStream());
int formDataLength=request.getContentLength();
字节数据字节[]=新字节[formDataLength];
int byteRead=0;
int totalBytesRead=0;
while(totalBytesRead  
System.out.println(“测试值为:”+名称);
%>
您已成功上载文件: 0){ System.out.println(“上传成功!”); } 否则{ System.out.println(“错误!”); } } 捕获(例外e){ e、 printStackTrace(); } } %>
提前感谢

请求。getParameter()及其相关方法不适用于多部分请求,在处理多部分表单数据时将始终返回null

如果要使用request.getParameter(),则可以使用commons FileUpload

试试看

编辑:

使用ApacheCommonsFileUpload的示例

FileItemFactory factory = new DiskFileItemFactory();

  // Set factory constraints
  // factory.setSizeThreshold(yourMaxMemorySize);
  // factory.setRepository(yourTempDirectory);

 // Create a new file upload handler
 ServletFileUpload upload = new ServletFileUpload( factory );
// upload.setSizeMax(yourMaxRequestSize);

// Parse the request
List<FileItem> uploadItems = upload.parseRequest( request );

for( FileItem uploadItem : uploadItems ){
     if( uploadItem.isFormField() ){
        String fieldName = uploadItem.getFieldName();
        String value = uploadItem.getString();
}
FileItemFactory工厂=新的DiskFileItemFactory();
//设置工厂约束
//setSizeThreshold(yourMaxMemorySize);
//setRepository(yourTempDirectory);
//创建新的文件上载处理程序
ServletFileUpload upload=新的ServletFileUpload(工厂);
//upload.setSizeMax(yourMaxRequestSize);
//解析请求
List uploadItems=upload.parseRequest(请求);
对于(FileItem uploadItem:uploadItems){
if(uploadItem.isFormField()){
String fieldName=uploadItem.getFieldName();
字符串值=uploadItem.getString();
}

}

这只是一个友好的建议-您应该真正阅读一些关于如何使
UploadFile.jsp
成为servlet的内容