gpt4 book ai didi

java - 问题上传图像 servlet - Eclipse,tomcat 服务器

转载 作者:行者123 更新时间:2023-11-28 23:33:07 25 4
gpt4 key购买 nike

<分区>

我有一个小问题,然后从一个 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”按钮,我将看到旧图像和一个小图标,然后是 img html 标记的路径不存在。如果我手动刷新我的图片所在的目录,并刷新网站,我将看到所有图片,包括新上传的图片。

为什么会出现这个问题?我该如何解决?

谢谢

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com