JSP文件上载程序以及表单输入不起作用

时间:2016-10-23 08:49:02

标签: java jsp

我试图在jsp中存储一个文件路径以及数据库中的一些值。问题是,当我尝试访问表单值(这是一个选择选项和文本输入),我试图显示它显示为null的值。

这是我的表单的jsp文件,这里动态下拉列表和ajax工作正常。

<form method="post" action="uploadfile.jsp" enctype="multipart/form-data" >
<div id="Layer1" style="position:absolute;text-align:left;left:373px;top:138px;width:608px;height:497px;z-index:17;">

<div id="wb_Text2" style="position:absolute;left:47px;top:35px;width:101px;height:18px;z-index:0;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:16px;">Department</span></div>
<div id="wb_Text3" style="position:absolute;left:47px;top:103px;width:81px;height:34px;z-index:1;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:16px;">Couse<br></span></div>
<div id="wb_Text1" style="position:absolute;left:48px;top:177px;width:98px;height:18px;z-index:2;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:16px;">Sem</span></div>
<div id="wb_Text4" style="position:absolute;left:49px;top:252px;width:77px;height:18px;z-index:3;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:16px;">Subject</span></div>
<div id="wb_Text5" style="position:absolute;left:49px;top:327px;width:76px;height:18px;z-index:4;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:16px;">File Type</span></div>
<input type="submit" id="Button2" name="" value="UPLOAD" style="position:absolute;left:353px;top:400px;width:116px;height:48px;z-index:5;">
<input type="submit" id="Button1" name="" value="VIEW" style="position:absolute;left:189px;top:400px;width:124px;height:48px;z-index:6;">
<select name="Combobox1" size="1" id="Combobox1" style="position:absolute;left:189px;top:313px;width:292px;height:32px;z-index:7;">
<option selected value="select">--Select--</option>
<option value="Pdf">df</option>
<option value="ppt">ppt</option>
<option value="doc">doc</option>
<option value="others">others</option>
</select>
<select name="dept" size="1" id="Combobox2" onchange="getId(this.value);" style="position:absolute;left:189px;top:35px;width:292px;height:32px;z-index:8;">

<option selected value="select" selected>--Select--</option>
<% 


         Connection conn=null;
  // String fname="select file_path from file1";
   String driverName ="oracle.jdbc.driver.OracleDriver";
         Class.forName(driverName);
         String serverName = "abcd";
         String serverPort ="2222";
         String sid= "XE";
         String url="jdbc:oracle:thin:@" +serverName+":"+serverPort+":"+sid;
         String username ="system";
         String password ="lokmnnd";
         conn= DriverManager.getConnection(url,username,password);
   Statement st=conn.createStatement();

   // ResultSet s=st.executeQuery("Select SUBSTR(file_path,INSTR(file_path,'\',-1,1)+1) as fname from file1");
  // ResultSet rt=st.executeQuery("select fname from s");
    ResultSet rs=st.executeQuery("Select * from dept"); 



while(rs.next())
{

    String id = rs.getString("did");
String fname = rs.getString("dname"); 


%>
<option value="<%=id %>"><%=fname %></option>
<%
}
%>
</select>




<select name="course" size="1" id="course" onchange="getIdq(this.value);" style="position:absolute;left:189px;top:105px;width:292px;height:32px;z-index:9;">
<option selected value="--Select">--Select--</option>
</select>
<select name="Combobox1" size="1" id="semester" onchange="getIde(this.value);"style="position:absolute;left:189px;top:177px;width:292px;height:32px;z-index:10;">
<option selected value="--Select--">--Select--</option>
</select>
<select name="Combobox1" size="1" id="subject" style="position:absolute;left:189px;top:245px;width:292px;height:32px;z-index:11;">
<option selected value="--Select--">--Select--</option>
</select>
<input type="file" name="photo"  style="position:absolute;left:189px;top:245px;width:292px;height:28px;z-index:11;"/>
<input type="text" name="filedesc"  style="position:absolute;left:189px;top:260px;width:292px;height:28px;z-index:11;"/>
</div>
</form>
</body>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
function getId(val)
{

        $.ajax({
        type:"POST",
        url:"courseselect.jsp",
        data:"uno="+val,
        success:function(data){
            $("#course").html(data);
        }
    });
}

function getIdq(val)
{

        $.ajax({
        type:"POST",
        url:"semesterselect.jsp",
        data:"uno="+val,
        success:function(data){
            $("#semester").html(data);
        }
    });
}

function getIde(val)
{

        $.ajax({
        type:"POST",
        url:"subjectselect.jsp",
        data:"uno="+val,
        success:function(data){
            $("#subject").html(data);
        }
    });
}
</script>

现在,当我尝试将值存储在数据库中时。表单字段的值被检索为null。这是我的uploadfile.jsp

<%@ page import="java.io.*,java.sql.*,java.util.zip.*,java.lang.*" %>

<%
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;
}
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("C:/UploadedFiles/"+saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
//fileOut.flush();
//fileOut.close();
}
%><br><table border="2"><tr><td><b>You have successfully upload the file:</b>
<%out.println(saveFile);%></td></tr></table>

<%
Connection connection = null;

PreparedStatement psmnt = null;

       // out.println("a");
       String a =request.getParameter("dept");
       out.println(a);
       // int no = Integer.parseInt(request.getParameter("course"));
        //out.println(no);

        //String b = request.getParameter("course");
      String c= request.getParameter("sem");
      String d= request.getParameter("subject");
      String e= request.getParameter("filetype");
      out.println('e');
      String f=request.getParameter("filedesc");
      out.print(f);

String driverName ="oracle.jdbc.driver.OracleDriver";
         Class.forName(driverName);
         String serverName = "abcd";
         String serverPort ="2222";
         String sid= "XE";
         String url="jdbc:oracle:thin:@" +serverName+":"+serverPort+":"+sid;
         String username ="system";
         String password ="lokmnnd";
         connection= DriverManager.getConnection(url,username,password);
psmnt = connection.prepareStatement("insert into ffile(department,course,sem,subject,filetype,fildes,filepath) values(?,?,?,?,?,?,?)");
psmnt.setString(7, ff.getPath());
psmnt.setString(1, a);
psmnt.setInt(2, no);
psmnt.setString(3, c);
psmnt.setString(4, d);
psmnt.setString(5, e);
psmnt.setString(6, f);


int s = psmnt.executeUpdate();
if(s>0){
System.out.println("Uploaded successfully !");
}
else{
System.out.println("Error!");
}

%>

执行uploadfile后,文件路径被完美上传,但从表单中检索的值,即变量a和f显示为null。 我无法弄清楚问题,请帮我解决这个问题。

0 个答案:

没有答案