0
我有兩個JSP頁面Form.jsp和UploadFile.jsp我怎樣才能得到一個jsp頁面圖片和文字到另一個JSP
Form.jsp
我想送圖像(fb.png )和像FNAME和LNAME值名稱是UploadFile.jsp頁
此之前,我試圖發送圖像後,我試圖讓FNAME,並通過文本框被檢索空值,我在成功
佛rm.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();
}
}
%>
感謝
只是一個友善的建議 - 你應該閱讀一些有關[MVC](HTTPS://en.wikipedia。 org/wiki/Model%E2%80%93view%E2%80%93controller),如何[避免scriplets](http://stackoverflow.com/questions/2188706/how-to-avoid-using-scriptlets-in- my-jsp-page),並可能考慮將'UploadFile.jsp'作爲一個servlet –