正如问题陈述所述,我无法通过servlet将图像上传到我的网站。我尝试通过CMS使人们有可能将新照片上传到网站首页的横幅。从理论上讲,该映像可以很好地上传(我在eclipse中的项目会自动更新,新映像已在我指示的文件夹中显示给我),并且路径已正确保存在数据库中。 问题是当我返回主页时,横幅在更改图像时会到达数据库中找到的新路径,并且图像不会加载。仅当我再次从eclipse运行项目时,这才“解决”;我想当网站安装在真实服务器上时会出现问题。
我将首先保留表单代码
<div id="foto" class="panel">
<form class="form" name="Frm-foto" method="post" action="${pageContext.request.contextPath}/SL_subir_foto_banner" enctype="multipart/form-data">
<table class="table table-hover table-heading table-datatable" id="datatable-1">
<tbody>
<tr>
<td>
<p>
<b>Estimado usuario, por favor atienda las siguientes recomendaciones para subir su foto:</b>
</p>
</td>
</tr>
<tr>
<td>
<ul>
<p>Foto de orientación horizontal.</p>
<p>Formato y dimensiones: el formato debe ser jpg, la foto debe de ser 1900 x 600 px para que se adecúe al tamaño del banner.</p>
</ul>
</td>
</tr>
<tr align="center">
<td>
<div class="cuadro-fotoNima" align="center">
<img id="preview" src="../../img/slides/nivo/foto-banner1.jpg" name="preview" alt="Foto NIMA"
style="width: 1000px; height: 350px; border-bottom-color: white; margin: 2px;" />
</div>
</td>
</tr>
<tr align="center">
<td>
<input type="file" id="foto" name="foto"
onchange="Test.UpdatePreview(this)" required="required">
<input type="hidden" name="idNIMA" value="">
<input type="hidden" name="codNIMA" value="">
</td>
</tr>
</tbody>
</table>
<input class="action" type="submit" value="Subir imagen"/>
</form>
</div>
现在是servlet代码。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try
{
DT_publicaciones dtpub = new DT_publicaciones();
Tbl_publicaciones tpub = new Tbl_publicaciones();
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
String path = getServletContext().getRealPath("/");
List<FileItem> items = upload.parseRequest(request);
File fichero = null;
File fichero2 = null;
String rutaFichero = null;
for(FileItem item : items)
{
FileItem uploaded = item;
if(!uploaded.isFormField())
{
/////////TAMAÑO DEL ARCHIVO ////////
long size = uploaded.getSize();
/////// GUARDAMOS EN UN ARREGLO LOS FORMATOS QUE SE DESEAN PERMITIR
List<String> formatos = Arrays.asList("image/jpeg");
////// COMPROBAR SI EL TAMAÑO Y FORMATO SON PERMITIDOS //////////
if(formatos.contains(uploaded.getContentType()))
{
int contador = dtpub.obtenerMenuOrder();
rutaFichero = "foto-banner"+contador+".jpg";
path = "C:\\Users\\pc\\git\\PortalWebHNMN\\PortalHNMN\\WebContent\\img\\slides\\nivo\\";
String url2 = path.substring(52);
url2 = url2.replace("\\", "/");
fichero = new File(path+rutaFichero);
///////// GUARDAR EN EL SERVIDOR //////////////
uploaded.write(fichero);
/////// ACTUALIZAMOS EL CAMPO URLFOTO EN LA BASE DE DATOS
String url = url2+rutaFichero;
tpub.setPublic_titulo(url);
if(dtpub.guardarBanner(tpub))
{
response.sendRedirect(request.getContextPath()+ "/CMS/menu/editBanner.jsp?msj=1");
}
else
{
response.sendRedirect(request.getContextPath()+ "/CMS/menu/editBanner.jsp?msj=2");
}
}
else
{
System.out.println("SERVIDOR: VERIFIQUE QUE EL ARCHIVO CUMPLA CON LAS ESPECIFICACIONES REQUERIDAS!!!");
response.sendRedirect(request.getContextPath()+ "/CMS/menu/editBanner.jsp?msj=3");
}
}
}
}
catch(Exception e)
{
System.out.println("SERVLET: ERROR AL SUBIR LA FOTO: " + e.getMessage());
}
}
请忽略以西班牙语编写的注释和其他内容,这是我的母语。我希望你能帮助我。