Jsp上传组件Smartupload介绍
【摘要】
<form action="UploadServlet" enctype="multipart/form-data" method="post"> 书籍名称:<input type="text"name="name"><br> 图片名称:上传文件:<input type="f...
-
<form action="UploadServlet" enctype="multipart/form-data" method="post">
-
书籍名称:<input type="text"name="name"><br>
-
图片名称:上传文件:<input type="file" name="img"><br/>
-
<input type="submit"value="提交">
-
</form>
-
package web;
-
-
-
import java.io.IOException;
-
import java.util.Date;
-
-
import javax.servlet.ServletException;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
import javax.servlet.jsp.JspFactory;
-
import javax.servlet.jsp.PageContext;
-
-
import com.jspsmart.upload.Request;
-
import com.jspsmart.upload.SmartUpload;
-
import com.jspsmart.upload.SmartUploadException;
-
-
public class UploadServlet extends HttpServlet {
-
-
private static final long serialVersionUID = 1L;
-
@Override
-
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
-
throws ServletException, IOException {
-
resp.setContentType("text/html");
-
//实例化SmartUpload上传组件
-
SmartUpload smart = new SmartUpload();
-
//PageContext pageContext=JspFactory.getDefaultFactory().getPageContext(this, req, resp, null, true, 8192, true);
-
//初始化上传操作
-
smart.initialize(this.getServletConfig(),req,resp);
-
smart.setMaxFileSize(1024*1024*10);
-
smart.setAllowedFilesList("jpg,jpeg,bmp");
-
//设置字符集
-
smart.setCharset("gbk");
-
//准备上传
-
try {
-
smart.setDeniedFilesList("exe,jsp,bat,html,,");
-
smart.upload();
-
Long time=new Date().getTime();
-
String ext=smart.getFiles().getFile(0).getFileExt();//获取文件后缀
-
String filename=time+"."+ext;
-
//smart.getFiles().getFile(0).saveAs( "/img/ "+filename);//保存在容器目录下
-
String pa=this.getServletContext().getRealPath("/")+"img"+java.io.File.separator+filename;
-
System.out.println(pa);
-
-
smart.getFiles().getFile(0).saveAs(pa);
-
// smart.save("img");
-
Request smartReq =smart.getRequest();
-
String name=smartReq.getParameter("name");
-
resp.getWriter().println(name);
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
//将上传的文件保存到项目根路径下的uploadfile目录中
-
}
-
@Override
-
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
-
throws ServletException, IOException {
-
this.doPost(req, resp);
-
}
-
}
<Context path="/web" docBase="D:\updemo\WebRoot"/>
在tomcat的host目录下设置,Context子标签,让上传的时候,同步上传到项目目录下!!!
文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。
原文链接:aaaedu.blog.csdn.net/article/details/53396683
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)