Create a form in the web page
<form action="/Blog/servlet/Shangchuan" method="post" enctype="multipart/form-data">
<input type="file" name="image" class="img">
<input type="submit" value="确认上传" class="blue">
</form>‘
Do the processing in the servlet
import java.io.IOException;
import java.io.PrintWriter;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class Shangchuan extends HttpServlet{
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException{
PrintWriter out=response.getWriter();
SmartUpload smartupload=new SmartUpload();
try{
smartupload.initialize(this.getServletConfig(),request,response);
smartupload.setAllowedFilesList("jpg,PNG,JPG");
smartupload.upload();
File file=smartupload.getFiles().getFile(0);
file.saveAs("/Image/"+file.getFileName(),File.SAVEAS_VIRTUAL);
out.print("上传成功");
}
catch(Exception e){
out.print("Upload failed, maybe the image format is wrong, temporarily only supports images ending in jpg, JPG, PNG");
}
}
}