您的位置:首页 > Web前端 > JavaScript

jspsmartupload 上传示例

2016-11-09 13:03 120 查看
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 设置编码格式
request.setCharacterEncoding("GB2312");
response.setCharacterEncoding("GB2312");
// 第一步:新建一个对象
SmartUpload smart = new SmartUpload();
// 第二步:初始化
smart.
4000
initialize(super.getServletConfig(), request, response);
// 第三步:设置上传文件的类型
smart.setAllowedFilesList("jpg,png");
try {
smart.setDeniedFilesList("bat");
smart.setMaxFileSize(10*1024*1024);
} catch (SQLException e1) {
e1.printStackTrace();
}
String name = null;
try {
smart.upload();
Files fs = smart.getFiles();// 得到所有的文件
for (int i = 0; i < fs.getCount(); i++) {// 对文件个数进行循环
File f = fs.getFile(i);
name = System.currentTimeMillis() + f.getFileName();
if (f.isMissing() == false) {
String path = request.getContextPath()+"/file/image/" + name;
//String path ="/yjdata/www/www/ROOT/file/image/" + name;
System.out.println("basePath="+path);
f.saveAs(path);
}
}
} catch (Exception e) {

e.printStackTrace();
}

int id  =Integer.parseInt(request.getParameter("id")) ;
if(AdminDAO.addimg(id, name)==1){
System.out.println("上传成功");
}
}
页面引用图片
<img height="130px" width="160px"  src= "<%=path%>/file/image/${apage.imgname}" alt="通用的占位符缩略图">
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: