首先,我很抱歉我的英语不是很好。我试图上传一个图像作为文件。当我在eclipse中尝试它时,我的代码工作得很好,但当我在浏览器上运行它时就不行了。我认为问题是浏览器在base64中转换图像。因此,没有文件名,它无法读取数据并将其写入磁盘。
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name= request.getParameter("name");
String description = request.getParameter("description");
String prix = request.getParameter("prix");
String image;
String chemin = getInitParameter( CHEMIN);
Part part = request.getPart("file");
String fileName =Paths.get(part.getSubmittedFileName()).getFileName().toString();
String fullPath = chemin + fileName;
if ( fileName != null && !fileName.isEmpty() ) {
fileName = fileName.substring( fileName.lastIndexOf( '/' ) + 1 )
.substring( fileName.lastIndexOf( '\\' ) + 1 );
fwrite( part, fileName, fullPath );
File srcFile = new File( fullPath );
byte [] buffer = new byte[ (int) srcFile.length() ];
try ( FileInputStream inputStream = new FileInputStream( srcFile ) ) {
inputStream.read( buffer );
}
}
image = fullPath;
manager.addBurger(name, description, prix, image);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(VUE);
dispatcher.forward(request, response);
}
我使用Java8,Tomcat8.5。你能帮帮我吗?我在很多论坛上搜索过,没有找到任何答案。
暂无答案!
目前还没有任何答案,快来回答吧!