我必须将文件上传到特定文件夹。这是接收参数并保存参数的jsp页面。
xyz.jsp
<%@page import="filepack.FileInfo"%>
<%@ page language="java" import="java.sql.*"
contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<html>
<body>
<jsp:useBean id="obj" class="filepack.FileInfo" scope="request" />
<jsp:setProperty name="obj" property="*"/>
<%@include file="upload.jsp" %>
<%
String uid = session.getAttribute("userId")+"";
String filetype=obj.getFiletype();
String fileName = uploadFile(application.getRealPath("/uploads") + "/" + uid +"/"+filetype,"", request);
boolean status = obj.saveFileInfo(fileName,uid);
if(status==true)
response.sendRedirect("uploadfiles.jsp?msg=File Saved&filtype="+filetype);
else
response.sendRedirect("uploadfiles.jsp?msg=File Not Saved");
%>
无法保存文件,因为filetype参数为null,除了fileName参数之外的其他参数也是如此。
我无法弄清楚为什么除fileName之外的所有参数都为null。请帮助我。
P.S: - 当我使用get方法时,会收到所有参数,但fileName参数为null。
这里是FileInfo.java
package filepack;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class FileInfo {
int fileid;
Date filedate;
String fileName;
String filetype;
String filedesc;
String sender;
String receiver;
String sendmode;
String sendstatus;
String receivestatus;
public boolean saveMyFileInfo(String fname,String userid)
{
FileInfo f=new FileInfo();
java.util.Date todayDate1 = new java.util.Date();
java.sql.Date todayDate = new java.sql.Date(todayDate1.getTime());
filedate=todayDate;
int n=0;
try
{
Connection cn = ConnectionManager.getConnection();
PreparedStatement ps = cn.prepareStatement("insert into myfileinfo(fileid,filedate,filename,filetype,filedesc,userid) values (?,?,?,?,?,?)");
String sql="select nvl(max(fileid),0)+1 from myfileinfo";
fileid=f.nextID(sql);
ps.setInt(1, fileid);
ps.setDate(2,filedate);
ps.setString(3, fname);
ps.setString(4,filetype);
ps.setString(5,filedesc);
ps.setString(6,userid);
n = ps.executeUpdate();
closeConnection(null, ps, cn);
}
catch(Exception e)
{
}
return n == 1;
}
public int saveFileInfo(String fname) throws Exception
{
Connection cn = ConnectionManager.getConnection();
PreparedStatement ps = cn.prepareStatement("Insert Into fileinfo(fileid,filename) values (?,?)");
String sql="select nvl(max(fileid),0)+1 from fileinfo";
fileid=nextID(sql);
ps.setInt(1, fileid);
ps.setString(2, fname);
int n = ps.executeUpdate();
closeConnection(null, ps, cn);
if (n == 1) {
return fileid;
} else {
return -1;
}
}
public int nextID(String sql)
{
int nid=0;
try
{
Connection cn = ConnectionManager.getConnection();
PreparedStatement ps = cn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
rs.next();
nid = rs.getInt(1);
closeConnection(null, ps, cn);
} catch (ClassNotFoundException | SQLException ex) {
} catch (Exception ex) {
}
return nid;
}
public int getFileid() {
return fileid;
}
public void setFileid(int fileid) {
this.fileid = fileid;
}
//....
}