0
這是行不通的。問題是我不知道甚至不知道應該發生什麼。我無法調試此代碼。我想將上傳文件存儲到臨時文件夾「temp」,然後將它們移動到「小程序」。請幫忙?該servlet顯然是被訪問,但我找不到上傳的文件...在此先感謝。使用Apache commons FileUpload
表(這是使用腳本小程序創建的 - 我把這個在這裏,如果可能導致問題):
<%
out.write("<p>Upload a new game:</p>");
out.write("<form name=\"uploadForm\" action=\"game.jsp\" "
+ "method=\"POST\" enctype=\"multipart/form-data\">"
+ "<input type=\"file\" name=\"uploadSelect\" value=\"\" width=\"20\" />"
+ "<br><input type=\"submit\" value=\"Submit\" name=\"uploadSubmitButton\" "
+ "onclick = \"submitToServlet2('UploadGameServlet');\">"
+ "</form>");
%>
其中要求此javascript:
function submitToServlet2(newAction)
{
document.uploadForm.action = newAction;
}
這反過來又進入到servlet (代碼全包,因爲可能有一些重要的元素隱藏)
package org.project;
import java.io.*;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
// import servlet stuff
import org.apache.commons.fileupload.*;
public class UploadGameServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
if (ServletFileUpload.isMultipartContent(request))
{
try
{
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List items = upload.parseRequest(request); /* FileItem */
File repositoryPath = new File("\\temp");
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
diskFileItemFactory.setRepository(repositoryPath);
Iterator iter = items.iterator();
while (iter.hasNext())
{
FileItem item = (FileItem) iter.next();
File uploadedFile = new File("\\applets");
item.write(uploadedFile);
}
}
catch (FileUploadException ex)
{
Logger.getLogger(UploadGameServlet.class.getName()).log(Level.SEVERE, null, ex);
}
catch (Exception ex)
{
Logger.getLogger(UploadGameServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet UploadGameServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet UploadGameServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
}
你不能調試servlet代碼嗎?你確定servlet正在被訪問嗎?我不知道是否需要在submitToServlet2函數中返回** true **以提交請求。 – 2008-10-22 16:15:07
是的,服務器輸出一個標題...所以我知道它至少得到輸出線。我如何調試servlet代碼? – pypmannetjies 2008-10-22 16:23:56