如何将一个jsp页面图像和文本添加到另一个jsp中

时间:2017-02-20 08:07:33

标签: javascript java html jsp servlets

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

Form.jsp

我想发送图像(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>

UploadFile.jsp

在此页面中,我将给定的图像保存到项目文件夹中并获取图像的路径。使用此代码,我可以检索图像,但我无法检索文本..我想检索图像和文本

UploadFile.jsp

<%@ 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();
}
}
%>

提前致谢

1 个答案:

答案 0 :(得分:0)

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

如果你想使用request.getParameter(),你可以使用commons FileUpload。

尝试。

编辑:

使用Apache Commons Fileupload的示例

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();
}

}