我想根据下拉列表中定义的类别将文件上传到指定的文件夹。我使用 o'reilly api 上传文件,这些文件会将文件存储到单个位置。我使用了 2 个下拉列表(主要类别和子类别)。如果用户定义了他的选项,那么路径应该改变,文件应该上传到指定的文件夹,例如:“c:\output\aero\int”。路径将是动态的,并且会根据用户 input.my 上传文件的代码
<%@page import="javax.ejb.DuplicateKeyException"%>
<%@ page import="java.io.*,java.sql.*,java.util.zip.*,com.oreilly.servlet.*,com.oreilly.servlet.MultipartRequest,javax.servlet.http.HttpServletResponse" %>
<!DOCTYPE html>
<%
ResultSet rs=null;
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/ksa";
PreparedStatement psmnt = null;
PreparedStatement psmnt1=null;
PreparedStatement psmnt2=null;
String path;
path="/home/adapco/Desktop/output";
MultipartRequest request2=new MultipartRequest(request,path,1024*1024*1024);
String filename=request2.getFilesystemName("file");
String file1=request2.getParameter("file1");
String selectedValue=request2.getParameter("sel1");
String select1=request2.getParameter("sel2");
String concat=selectedValue+"." +select1;
File f=request2.getFile("file");
//out.println(f.exists()+"----------------"+f.getAbsolutePath());
//out.print(filename);
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
try
{
java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
psmnt1 = connection.prepareStatement("insert into file12(id,file_description,file_path,file_date) values(?,?,?,'"+ sqlDate+"')");
psmnt1.setString(1, concat);
psmnt1.setString(2, file1);
psmnt1.setString(3, f.getPath());
//psmnt.setString(3, "sqlDate");
//String query="select * from file12";
int s=psmnt1.executeUpdate();
if(s>0)
{
System.out.println("Uploaded successfully !");
}
else
{
System.out.println("Error!");
}
psmnt=connection.prepareStatement("select * from file12");
rs=psmnt.executeQuery();
}
catch(Exception e)
{
out.print(" the file already exist"+e);
}
%>
<table cellpadding="15" border="1">
<%
while(rs.next()){
%>
<tr>
<td><%=rs.getString(1)%> </td>
<td><%=rs.getString(2)%> </td>
<td><%=rs.getString(3)%> </td>
<td><%=rs.getString(4)%></td>
</tr>
<%}%>
<a href="index.jsp">home</a><br>
<a href="sample.jsp">upload file</a>
</table>
<%
response.sendRedirect("index.jsp");
%>
带有下拉菜单的设计部分:
<FORM ENCTYPE="multipart/form-data" ACTION="sample2.jsp" METHOD=POST>
<center>
<table bgcolor=#38ACEC>
<tr>
<center><td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
</tr>
<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><b>Choose the destination folder:</b></td>
<td><INPUT NAME="file1" TYPE="file"></td>
</tr>-->
<tr><td><select name="sel1">
<option value="1">Aerospace</option>
<option value="2">Automotive</option>
<option value="3">Energy</option>
<option value="4">IC Engines</option>
<option value="5">Wind</option>
<option value="6">Turbo</option>
<option value="7">IT</option>
<option value="8">Training</option>
</select>
<br>
<select name="sel2">
<option value="1">Internal</option>
<option value="2">Demos</option>
<option value="3">Best Practice</option>
<option value="4">Marketing</option>
<option value="5">Papers & public</option>
<option value="6">Validation</option>
<option value="7">Training</option>
</select></td></tr>
<tr><td colspan="2" align="center"> </td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="Send File"> </td></tr>
</table>
</center>
</form>
我想做的是:
MultipartRequest request2=new MultipartRequest(request,"/home/adapco/Desktop/output",1024*1024*1024);
String filename=request2.getFilesystemName("file");
String file1=request2.getParameter("file1");
String selectedValue=request2.getParameter("sel1");
String select1=request2.getParameter("sel2");
String concat=selectedValue+"." +select1;
File f=request2.getFile("file");
//out.println(f.exists()+"----------------"+f.getAbsolutePath());
//out.print(filename);
Double double1=Double.parseDouble(concat);
if(double1==1.1)
{
request2=new MultipartRequest(request,"/home/adapco/Desktop/output/aero/int",1024*1024*1024);
}
上面的代码显示错误
java.io.IOException: Corrupt form data: premature ending
如果仅创建了多部分对象,它会显示给我
request2 is not initialized.
我正在比较串联值以获得用户定义的类别。