我想将文件及其详细信息从jsp页面上传到服务器,并使用对servlet的 ajax 请求将其存储在数据库中。我的代码正常工作,并且仅在第一次提交数据。但是它无法为后续的提交操作提交数据。有人可以帮我弄清楚这里的错误。 我的 jsp 文件中有一个表单,它看起来像:
<form method="POST" id="form_id">
<label for="header">Tip Header:</label><input type="text" id="header" name="header"><br>
<label for="msg">Tip Message:</label><input type="text" id="msg" name="msg"><br>
<label for="file_upload">Tip Image:</label><input type="file" name="file_upload" id="file_upload" style="width:500px; font-size: 15px;"><br><br>
<label for="link">Tip Link:</label><input type="text" id="link" name="link"><br>
</form>
<button id="add">ADD</button>
<button type="reset" id="clear">Clear</button>
Ajax调用:
document.getElementById("add").onclick=function()
{
var header=document.getElementById('header').value;
var msg=document.getElementById('msg').value;
var file_upload=document.getElementById('file_upload').files[0];
var link=document.getElementById('link').value;
var formData=new FormData();
formData.append("header",header);
formData.append("msg",msg);
formData.append("file_upload",file_upload);
formData.append("link",link);
var xhtp=new XMLHttpRequest();
xhtp.onreadystatechange=function()
{
if(this.readyState==4 && this.status==200)
{
alert(xhtp.responseText);
}
};
xhtp.open("POST","/TipsTab/addData",true);
xhtp.send(formData);
}
Servlet:
public class UploadImg extends HttpServlet
{
private String filepath;
private int maxFileSize = 500 * 1024;
private int maxMemSize = 400 * 1024;
private File file ;
private static String URL="jdbc:mysql://localhost/pictures",className="com.mysql.jdbc.Driver",user="root",pass="";
private static Connection con;
private static PreparedStatement ins=null;
private static String ins_query="insert into tips values(null,?,?,?,?,?)";
private int i=1;
public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
{
filepath=getServletContext().getInitParameter("file-upload");
if(ServletFileUpload.isMultipartContent(request))
{
DiskFileItemFactory factory=new DiskFileItemFactory();
factory.setSizeThreshold(maxMemSize);
factory.setRepository(new File("/Users/test/Documents/OutputImages/extra"));
ServletFileUpload upload=new ServletFileUpload(factory);
upload.setSizeMax(maxFileSize);
try
{
Class.forName(className);
con=DriverManager.getConnection(URL,user,pass);
ins=con.prepareStatement(ins_query);
List<FileItem> items=upload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
ins.setString(i++,value);
}
else {
String fieldName = item.getFieldName();
ins.setString(i++,item.getName());
String fileName = new File(item.getName()).getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
String path=filepath+File.separator+fileName;
ins.setString(i++,path);
file=new File(path);
item.write(file) ;
}
}
int n=ins.executeUpdate();
if(n>0)
{
response.getWriter().write(n+" record entered");
}
if(con!=null)
{
con.close();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}