我正在开发一个Web应用程序,其中我有一个带有表单的Index.jsp,用于将文件上传到servlet:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
import="com.uteva.AppWebNieve.ProcesadoExcel"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets" lang="es" xml:lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
<title>AppWebInformesNieve</title>
</meta>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input value="Subir Excel" type="submit" />
</form>
</body>
</html>
然后我用我的java类正确上传文件:
@Override
protected void doPost(HttpServletRequest
request,HttpServletResponse response) throws ServletException,
IOException{
//***************AQUI MANEJAMOS LA SUBIDA DEL FICHERO LINCES DE INCIDENCIAS***********************
// Get the file location where it would be stored.
//String filePath = getServletContext().getInitParameter("file-upload");
boolean isMultipart;
int maxFileSize = 10000 * 1024;
int maxMemSize = 1000 * 1024;
File file ;
// Check that we have a file upload request
isMultipart = ServletFileUpload.isMultipartContent(request);
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter( );
if( !isMultipart ){ //No se ha podido subir el fichero
/*out.println("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>No file uploaded</p>");
out.println("</body>");
out.println("</html>");*/
return;
}
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(maxMemSize);
// Location to save data that is larger than maxMemSize.
factory.setRepository(new File("c:\\temp"));
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum file size to be uploaded.
upload.setSizeMax( maxFileSize );
try{
// Parse the request to get file items.
List fileItems = upload.parseRequest(request);
// Process the uploaded file items
Iterator i = fileItems.iterator();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
while ( i.hasNext () )
{
FileItem fi = (FileItem)i.next();
if ( !fi.isFormField () )
{
// Get the uploaded file parameters
String fieldName = fi.getFieldName();
String fileName = fi.getName();
String contentType = fi.getContentType();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();
// Write the file
if( fileName.lastIndexOf("\\") >= 0 ){
file = new File( filename +
fileName.substring( fileName.lastIndexOf("\\"))) ;
}else{
file = new File( filename +
fileName.substring(fileName.lastIndexOf("\\")+1)) ;
}
fi.write( file ) ;
out.println("Uploaded Filename: " + fileName + "<br>");
System.out.println("Fichero "+filename+" subido correctamente al servidor!!");
}
}
out.println("</body>");
out.println("</html>");
}catch(Exception ex) {
System.out.println(ex);
}
}
但是在上传之后,这打开了一个新的简单html,它是由out.println在以前的java方法中构建的......
重点是,我如何管理这个上传以保持相同的HTML知道文件是否已上传或不使用此文件做其他事情?我不能使用get方法上传文件并让表单操作空白...
或者,打开一个新的html,必须有另一种方法来制作比Java函数中使用out.println更复杂的html ....
感谢。
答案 0 :(得分:0)
最后,我按照本教程使用AJAX解决了这个问题:http://www.programming-free.com/2013/06/ajax-file-upload-java-iframe.html