表单是由javascript提交的,但是servlet向jsp返回一个null属性,我认为这行request.getRequestDispatcher(“views / Upload.jsp”)forward(request,response)没有运行。我留下代码:
Upload.jsp
<form id="upload" method="post" action="../Upload_Servlet" enctype="multipart/form-data">
<div id="drop">
Drop Here
<a>Browse</a>
<input type="file" id="file" name="file"/>
<input name="fileName" id="fileName" type="text" style="visibility: hidden" value="<%=request.getAttribute("logotipo")%>">
</div>
<ul>
<!-- The file uploads will be shown here -->
</ul>
<div id="tudiv"></div>
<input type="button" id="Aceptar" value="Aceptar">
</form>
Upload_Servlet.java
if (!fi.isFormField()) {
// Get the uploaded file parameters
String fieldName = fi.getFieldName();
String fileName = Math.floor(Math.random() * (1000 - 1) + 1) + fi.getName();
request.setAttribute("logotipo", fileName);
String contentType = fi.getContentType();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();
// Write the file
if (fileName.lastIndexOf("\\") >= 0) {
file = new File(filePath
+ fileName.substring(fileName.lastIndexOf("\\")));
} else {
file = new File(filePath
+ fileName.substring(fileName.lastIndexOf("\\") + 1));
}
fi.write(file);
out.println("Archivo subido: " + fileName + "<br>");
request.getRequestDispatcher("vistas/Upload.jsp").forward(request, response);
}
的script.js
$('#upload').fileupload({
// This element will accept file drag/drop uploading
dropZone: $('#drop'),
// This function is called when a file is added to the queue;
// either via the browse button, or via drag/drop:
add: function (e, data) {
var tpl = $('<li class="working"><input type="text" value="0" data-width="48" data-height="48"'+
' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>');
// Append the file name and file size
tpl.find('p').text(data.files[0].name)
.append('<i>' + formatFileSize(data.files[0].size) + '</i>');
// Add the HTML to the UL element
data.context = tpl.appendTo(ul);
// Initialize the knob plugin
tpl.find('input').knob