我正在尝试使用 servlet 将文件上传到 WebLogic 服务器。这是 doPost 方法:
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException
{
boolean isMultipart;
String filePath=null;
int maxFileSize = 2 * 1024 * 1024;
int maxMemSize = 1024 * 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 )
{
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(2 * 1024 * 1024);
// 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();
ServletContext context = getServletContext();
Connection conn = (Connection)context.getAttribute("DBCON");
PreparedStatement stmt=null;
try
{
stmt=conn.prepareStatement("INSERT INTO emp VALUES(?, ?, ?)");
out.println("Created the statement!");
}
catch (SQLException e)
{
out.println("Unable to create prepared statement because: "+e.getMessage());
}
WriteToDB ob=new WriteToDB();
out.println("CWriting to DB!");
ob.process("C:\\JDeveloper\\mywork\\App\\Project1\\data" + "\\" + fileName, stmt, out);
out.println("Wrote to DB!");
}
}
} catch (FileUploadException e) {
}
out.println("</body>");
out.println("</html>");
}
正如我所读到的,这是 web.xml 的重要部分:
<context-param>
<description>Location to store uploaded file</description>
<param-name>file-upload</param-name>
<param-value>
C:\JDeveloper\mywork\App\Project1\data
</param-value>
我正在使用 JDeveloper。
我有一个带有表单数据的标准上传 HTML 页面。一切都很好,除了在存储位置找不到该文件,它会引发该异常。请帮忙,谢谢。