我编写了一个 Servlet 来将文件上传到服务器并存储它们。只要我运行 DevelopmentMode (这是一个 GWT 项目),一切正常。但是由于我在 Tomcat 上部署了 webapp,所以甚至没有调用 servlet..
public class ImageService extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final long MAX_FILE_SIZE = 1024 * 1024 * 1024; //1GB
@Override
protected void doPost(final HttpServletRequest request,
final HttpServletResponse response) {
slog("SERVLET STARTED");
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
slog("REQUEST IS MULTIPART");
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("text/html");
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(MAX_FILE_SIZE);
try {
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> iterator = items.iterator();
while (iterator.hasNext()) {
FileItem item = iterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
slog("TROVATO FILE " + item.getName());
String root = getServletContext().getRealPath("/");
File path = new File(root + "/fileuploads");
slog("SALVO FILE IN " + path.getAbsolutePath());
if (!path.exists()) {
path.mkdirs();
}
File uploadedFile = creaFileNonAmbiguo(path, fileName);
slog("NOME ASSEGNATO AL FILE " + uploadedFile.getName());
item.write(uploadedFile);
response.getWriter().write(uploadedFile.getName() + ";");
}
}
response.getWriter().flush();
slog("RISPOSTA INVIATA");
} catch (Exception e) {
e.printStackTrace();
}
} else {
slog("LA RICHIESTA NON E' MULTIPART");
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
slog("SERVLET TERMINATA");
}
@Override
protected void doGet(final HttpServletRequest request,
final HttpServletResponse response) {
response.setContentType("image/jpeg");
String root = getServletContext().getRealPath("/").concat(
"fileuploads/");
String path = root.concat(request.getParameter("src"));
File file = new File(path);
response.setContentLength((int) file.length());
FileInputStream in;
try {
in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) >= 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private File creaFileNonAmbiguo(File path, String fileName) {
File res = new File(path + "/" + fileName);
if (!res.exists())
return res;
else {
return creaFileNonAmbiguo(path, "c".concat(fileName));
}
}
private void slog(String s) {
System.out.println("FILE SERVLET: " + s);
}
}
我的代码有什么问题?为什么它在 devmode 下工作而不是在 Tomcat 上工作?
这是我的 Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<!-- Servlets -->
<servlet>
<servlet-name>dataLayerServlet</servlet-name>
<servlet-class>com.tesi.server.DataLayerServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dataLayerServlet</servlet-name>
<url-pattern>/segnalazioni_degrado/dataLayer</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.tesi.server.LoginServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/segnalazioni_degrado/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>mailServlet</servlet-name>
<servlet-class>com.tesi.server.MailServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mailServlet</servlet-name>
<url-pattern>/segnalazioni_degrado/mail</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>imageServlet</servlet-name>
<servlet-class>com.tesi.server.ImageService</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>imageServlet</servlet-name>
<url-pattern>/segnalazioni_degrado/imageUpload</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>Segnalazioni_Degrado.html</welcome-file>
</welcome-file-list>