最近做了个项目,用到了图片上传,技术是别人的,我拿来用了哈,恩。。代码实现。
先在JSP页面写个简单的上传按钮:
<body bgcolor="#ECFFFF">
<center>
请添加书籍图片:
<form encType='multipart/form-data' action='servlet/SelectBookImgServlet' method='post'>
<input type='file' name='book'/>
<input type='submit' value='确定'>
</form>
</center>
</body>
----这边要注意的就是得在form表单属性中加encType='multipart/form-data'
还有就是<input type='file'..../>这里不能不写/,否则无法正确结束。
-------在SelectBookImgServlet中实现图片上传:
public class SelectBookImgServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String str="\\webdb\\bookupload\\auto.jpg"; //这里可以指定默认图片的路径,也就是不上传图片直接点确定时默认上传的图片
try {
ServletContext application = this.getServletContext(); // 创建一个servlet容器级对象
String path = application.getRealPath("/bookupload");
java.io.File file = new File(path);
System.out.print(path);
if (!file.exists())
{
file.mkdir();
}
SmartUpload smart = new SmartUpload(); // 创建一个上传文件对象
smart.initialize(this.getServletConfig(), request, response);
smart.upload(); // 上传这个文件
for (int i = 0; i < smart.getFiles().getCount(); i++) // 遍历这个smart中所有文件
{
com.jspsmart.upload.File ff = smart.getFiles().getFile(i); // 取出smart内部文件
if (!ff.isMissing()) {
String fileName = ff.getFileName();
str="\\webdb\\bookupload\\" + fileName;
ff.saveAs(path + "\\" + fileName,
com.jspsmart.upload.File.SAVEAS_PHYSICAL);// 第一个参数保存的路径,第二个参数保存的物理地址
}
}
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setAttribute("str", str); //这里也就是把图片的站点路径传过去,是为了把路径存入数据库,方便读取
request.getRequestDispatcher("/admin/book_operate/book_insert.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
先在JSP页面写个简单的上传按钮:
<body bgcolor="#ECFFFF">
<center>
请添加书籍图片:
<form encType='multipart/form-data' action='servlet/SelectBookImgServlet' method='post'>
<input type='file' name='book'/>
<input type='submit' value='确定'>
</form>
</center>
</body>
----这边要注意的就是得在form表单属性中加encType='multipart/form-data'
还有就是<input type='file'..../>这里不能不写/,否则无法正确结束。
-------在SelectBookImgServlet中实现图片上传:
public class SelectBookImgServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String str="\\webdb\\bookupload\\auto.jpg"; //这里可以指定默认图片的路径,也就是不上传图片直接点确定时默认上传的图片
try {
ServletContext application = this.getServletContext(); // 创建一个servlet容器级对象
String path = application.getRealPath("/bookupload");
java.io.File file = new File(path);
System.out.print(path);
if (!file.exists())
{
file.mkdir();
}
SmartUpload smart = new SmartUpload(); // 创建一个上传文件对象
smart.initialize(this.getServletConfig(), request, response);
smart.upload(); // 上传这个文件
for (int i = 0; i < smart.getFiles().getCount(); i++) // 遍历这个smart中所有文件
{
com.jspsmart.upload.File ff = smart.getFiles().getFile(i); // 取出smart内部文件
if (!ff.isMissing()) {
String fileName = ff.getFileName();
str="\\webdb\\bookupload\\" + fileName;
ff.saveAs(path + "\\" + fileName,
com.jspsmart.upload.File.SAVEAS_PHYSICAL);// 第一个参数保存的路径,第二个参数保存的物理地址
}
}
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setAttribute("str", str); //这里也就是把图片的站点路径传过去,是为了把路径存入数据库,方便读取
request.getRequestDispatcher("/admin/book_operate/book_insert.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}