Crear un formulario en la página web
<form action="/Blog/servlet/Shangchuan" method="post" enctype="multipart/form-data">
<input type="file" name="image" class="img">
<input type=" enviar" valor="确认上传" clase="azul">
</formulario>'
Hacer el procesamiento en el servlet.
importar java.io.IOException;
importar java.io.PrintWriter;
importar java.io.PrintWriter;
importar javax.servlet.ServletException;
importar javax.servlet.http.HttpServlet;
importar javax.servlet.http.HttpServletRequest;
importar javax.servlet.http.HttpServletResponse;
importar javax.servlet.http.HttpServlet;
importar javax.servlet.http.HttpServletRequest;
importar javax.servlet.http.HttpServletResponse;
importar com.jspsmart.upload.File;
importar com.jspsmart.upload.SmartUpload;
importar com.jspsmart.upload.SmartUploadException;
importar com.jspsmart.upload.SmartUpload;
importar com.jspsmart.upload.SmartUploadException;
clase pública Shangchuan extiende HttpServlet {
privado estático final largo serialVersionUID = 1L;
public void doPost (solicitud HttpServletRequest, respuesta HttpServletResponse) lanza ServletException, IOException{
PrintWriter out=response.getWriter();
SmartUpload smartupload=nuevo SmartUpload();
prueba{
smartupload.initialize(this.getServletConfig(),solicitud,respuesta);
smartupload.setAllowedFilesList("jpg,PNG,JPG");
carga inteligente.subir();
Archivo archivo=smartupload.getFiles().getFile(0);
file.saveAs("/Imagen/"+file.getFileName(),File.SAVEAS_VIRTUAL);
out.print("上传成功");
}
catch(Excepción e){
out.print("Carga fallida, tal vez el formato de la imagen es incorrecto, temporalmente solo admite imágenes que terminan en jpg, JPG, PNG");
}
}
}