-1
我想使用JSP上傳文件,但我收到並且錯誤並且沒有文件上傳。FileUpload不能在JSP中工作
我用於上傳的Servlet代碼。
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.RequestContext;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;
/**
*
* @author Hemal
*/
@MultipartConfig
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String DATA_DIRECTORY = "data";
private static final int MAX_MEMORY_SIZE = 1024 * 1024 * 2;
private static final int MAX_REQUEST_SIZE = 1024 * 1024;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
File file;
int maxFileSize = 5000 * 1024;
int maxMemSize = 5000 * 1024;
ServletContext context = request.getServletContext();
String filePath = context.getInitParameter("file-upload");
PrintWriter out=response.getWriter();
// Verify the content type
String contentType = request.getContentType();
if ((contentType.contains("multipart/form-data"))) {
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("data"));
// 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);//ERROR ON THIS LINE
// Process the uploaded file items
Iterator i = fileItems.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
if (!fi.isFormField()) {
// Get the uploaded file parameters
String fieldName = fi.getFieldName();
String fileName = fi.getName();
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("Uploaded Filename: " + filePath
+ fileName + "<br>");
}
}
} catch (Exception ex) {
System.out.println(ex);
}
}
}
}
錯誤我收到線
`List fileItems = upload.parseRequest(request);`
是 HttpServletRequest cannot be converted to RequestContext
我試過很多的選擇,但沒有成功。
謝謝。
編輯
在JSP文件上傳相關鏈接只顯示部分代碼,不保存實際的文件到用戶定義的目錄。我想要一個代碼來保存一個文件也是一個目錄。所以我認爲這不是一個重複的問題。
您必須顯示錯誤消息。 – Elvermg
[使用Apache Tomcat 7.0.40.0時servlet中的文件上傳錯誤]的可能重複(http://stackoverflow.com/questions/16768086/file-upload-error-in-servlet-while-using-apache-tomcat-7 -0-40-0)或 http://stackoverflow.com/questions/17590108/jsp-upload-file-failed – Taha
@Elvermg我已經顯示了問題本身的錯誤信息。 – Hemal