我正在尝试使用以下代码上传文件:
package controle;
@WebServlet("/upload")
@MultipartConfig
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
String id;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
uploadFile(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
id = (String) request.getAttribute("id");
uploadFile(request, response);
}
private void uploadFile(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
for (Part part : request.getParts()) {
String name = part.getName();
InputStream is = request.getPart(name).getInputStream();
String fileName = getUploadedFileName(part);
FileOutputStream fos = new FileOutputStream(
"//C:/Users/achraf/workspace19/guidepro/WebContent/WEB-INF/imagesapp/profileapp"
+ fileName);
int data = 0;
while ((data = is.read()) != -1) {
fos.write(data);
}
fos.close();
is.close();
response.sendRedirect("success.jsp");
}
}
private String getFormat(String fileName) {
String format = "none";
int index = fileName.lastIndexOf(".");
if (index > 0) {
format = fileName.substring(index + 1);
format = format.toLowerCase();
}
return format;
}
private String getUploadedFileName(Part p) {
String file = "", header = "Content-Disposition";
String[] strArray = p.getHeader(header).split(";");
for (String split : strArray) {
if (split.trim().startsWith("filename")) {
file = split.substring(split.indexOf('=') + 1);
file = file.trim().replace("\"", "");
System.out.println("File name : " + file);
}
}
return file;
}
}
但是当我的应用程序运行时,我收到此错误消息:
java.io.FileNotFoundException: C:\Users\achraf\workspace19\guidepro\WebContent\WEB-INF\imagesapp\profileapp (Accès refusé)
java.io.FileOutputStream.open(Native Method)
java.io.FileOutputStream.<init>(Unknown Source)
java.io.FileOutputStream.<init>(Unknown Source)
controle.FileUploadServlet.uploadFile(FileUploadServlet.java:45)
controle.FileUploadServlet.doPost(FileUploadServlet.java:31)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
出了什么问题????
答案 0 :(得分:0)
确保在WEB-INF下有一个名为imagesapp的文件夹,就像你有
一样//C:/Users/achraf/workspace19/guidepro/WebContent/WEB-INF/imagesapp/profileapp"
这意味着它会将fileName
附加到profileapp
并将其存储在imagesapp
中。但如果您假设该文件将保留在profileapp下,那么只需将斜杠添加到它像这样
C:/Users/achraf/workspace19/guidepro/WebContent/WEB-INF/imagesapp/profileapp/"
答案 1 :(得分:0)
在uploadFile方法中:
String name = part.getName();
InputStream is = request.getPart(name).getInputStream();
您已经拥有获取输入流的部分,并使用getPart(name)获取上面的相同实例部分。
我不建议将输入流写入带有某个输出流的文件。 相反,如果它执行以下操作:
String fileName = part.getHeader("content-disposition").replaceAll(".+filename=\"(.+)\"", "$1");
String fileFormat = fileName.replaceAll(".+[.](.+)$", "$1");
part.write("//C:/Users/achraf/workspace19/guidepro/WebContent/WEB-INF/imagesapp/profileapp/" + filename);
请注意:
"//C:/Users/achraf/workspace19/guidepro/WebContent/WEB-INF/imagesapp/profileapp"
+ fileName
就是你做的。 profileapp中缺少最终/。
邮件错误抛出:
java.io.FileNotFoundException: C:\Users\achraf\workspace19\guidepro\WebContent\WEB-INF\imagesapp\profileapp
profileapp是一个文件夹。该消息显示您的方法返回一个空字符串,定义为&#34;&#34;在方法的开头。如果您的方法返回&#34; SOME_STRING&#34;,如果文件夹imagesapp不存在,您将看到以下消息错误:
java.io.FileNotFoundException: C:\Users\achraf\workspace19\guidepro\WebContent\WEB-INF\imagesapp\profileappSOME_STRING
否则,您将在imagesapp文件夹中创建profileappSOME_STRING文件。
如果您的profileapp文件夹不存在,您可以毫无例外地在imagesapp文件夹中创建文件profileapp。并不是说你有拒绝访问权限。您正尝试将文件夹中的内容写为文件。