我有一点问题,然后从servlet上传文件。 上传成功,但我必须手动刷新我的图像目录(在WebContent中)以查看网站上传的图像。
这里是我选择文件的表格:
<form action="SRVLTInsererImage" method="POST" enctype="multipart/form-data">
<div>
<div>
<label>Choisir une catégorie</label>
<select name="id_categorie">
<c:forEach var="item" items="${categories}">
<option value="${item.idCategorie}">${item.nomCategorie}</option>
</c:forEach>
</select>
</div>
<div>
<label>Titre</label>
<input type="text" name="nom_image" />
</div>
<div>
<label>Description</label>
<textarea name="desc_image"></textarea>
</div>
</div>
<div>
<input type="file" name="file" size="1024" multiple="multiple"/>
<input type="submit" name="insertimage" value="Upload Fichier" />
</div>
</form>
这里是我上传的servlet的代码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String idcategorie = "null";
String nomImage = "Pas de titre";
String descImage = "Pas de description";
String urlImage = "nullurl";
isMultipart = ServletFileUpload.isMultipartContent(request);
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter( );
if( !isMultipart ){
//redirection vers jsp de "pas d'upload"
}
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(maxMemSize);
factory.setRepository(new File("c:\\temp"));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax( maxFileSize );
try{
List fileItems = upload.parseRequest(request);
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();
String contentType = fi.getContentType();
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)) ;
}
urlImage = file.getName();
fi.write( file ) ;
}
else
{
String fieldname = fi.getFieldName();
String fieldvalue = fi.getString();
if (fieldname.equals("id_categorie"))
idcategorie = fieldvalue;
else if (fieldname.equals("nom_image"))
nomImage = fieldvalue;
else if (fieldname.equals("desc_image"))
descImage = fieldvalue;
}
}
//here i adding the URLimage with hibernate in the database
RequestDispatcher req = request.getRequestDispatcher("/index.jsp");
req.forward(request, response);
}catch(Exception ex) {
System.out.println(ex);
}
}
所以,然后我提交你看到的表格,我将重定向index.jsp,如果我点击“showAllImage”按钮我将看到旧图像和一点icone然后img html标签的路径不存在。如果我手动刷新我的图像所在的目录,并刷新网站,我将看到所有图像,包括新上传的图像。
为什么会出现这个问题?我怎么解决呢?
感谢
答案 0 :(得分:0)
我解决了问题=&gt; tomcat是克隆目录,这个目录将是dynamique,il链接到这个目录,它的工作原理。如果有人想要更多解释,请问我