У меня есть источник, который, я думаю, должен работать, но по какой-то причине он дает мне ресурс, который не найден, и совершенно другой ресурс.
HTML часть, просто форма:
<html lang="en">
<head>
<title>File Uploader</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method="POST" action="upload" enctype="multipart/form-data" >
File:
<input type="file" name="file" id="file" />
<input type="submit" value="Upload" name="upload" id="upload" />
</form>
</body>
</html>
Java часть:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
@WebServlet(name = "FileUploader", urlPatterns = "upload")
@MultipartConfig
public class FileUploader extends HttpServlet {
private final static String serverPath = "/fileuploads";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("text/html;charset=UTF-8");
final Part filePart = request.getPart("file");
String fileName = getFileName(filePart);
OutputStream out = null;
InputStream filecontent = null;
final PrintWriter writer = response.getWriter();
try {
out = new FileOutputStream(new File(serverPath + File.separator + fileName));
filecontent = filePart.getInputStream();
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = filecontent.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
writer.println("New file " + fileName + " created at " + serverPath);
} catch (FileNotFoundException fne) {
writer.println("Missing file or no insufficient permissions.");
writer.println(" ERROR: " + fne.getMessage());
} finally {
if (out != null) {
out.close();
}
if (filecontent != null) {
filecontent.close();
}
if (writer != null) {
writer.close();
}
}
}
private String getFileName(Part filePart) {
String header = filePart.getHeader("content-disposition");
String name = header.substring(header.indexOf("filename=\"")+10);
return name.substring(0, name.indexOf("\""));
}
}
Я ожидаю, что файл будет загружен в / proj / publ / fileuploads, но в инстаграме указано, что ресурс / proj / publ / uploads недоступен ....
Файлы находятся в папке / proj / publ /. Почему он всегда указывает на ту папку, которая не существует?
Спасибо за вашу помощь.
Viking
РЕДАКТИРОВАТЬ: Проблема решена ... по какой-то причине я создал файл Java в src, а не в WEB-INF / src ... поэтому возникла проблема.