jspsmartupload类实现控制文件上传类型(如*.gif,*.jpg)的代码???

phptong 2004-09-01 03:38:24
如上所问,希望大虾解答,多谢先
...全文
187 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
gggggame 2004-11-24
  • 打赏
  • 举报
回复
晕 原来不来这里问就对了 有总是先找google
zhushizu 2004-09-01
  • 打赏
  • 举报
回复
您可以到Google上搜一下呀,这方面的资料还是多的..但一定要有耐心...楼主加油了!!!!!
bjblz 2004-09-01
  • 打赏
  • 举报
回复
记得结贴哦
phptong 2004-09-01
  • 打赏
  • 举报
回复
可以了,会出现一个异常
多谢
phptong 2004-09-01
  • 打赏
  • 举报
回复
我试了,不行,其他类型的文件还是可以上传
还有其他方法吗?
Ronanljy 2004-09-01
  • 打赏
  • 举报
回复
SmartUpload smartUpload = new SmartUpload();
smartUpload.setAllowedFilesList("gif,jpg");
package com.etoak.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.etoak.dao.UploaderDaoIf; import com.etoak.dao.UploaderDaoImpl; import com.etoak.po.Uploader; import com.etoak.util.UUIDGenerator; import com.jspsmart.upload.File; import com.jspsmart.upload.Files; import com.jspsmart.upload.Request; import com.jspsmart.upload.SmartUpload; public class Upload extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=gbk"); request.setCharacterEncoding("gbk"); try { // 1:引入smartupload SmartUpload su = new SmartUpload(); // 2:设置允许上文件的后缀名,用逗号隔开 su.setAllowedFilesList("jpg,gif,bmp,jpeg"); // 3:设置允许上文件的大小 .这里是3m su.setMaxFileSize(3 * 1024 * 1024); // 4:初始化,接受页面递过来的请求 su.initialize(getServletConfig(), request, response); // 5:上 su.upload(); // 拿取Smartupload的request // 注意当我们使用了SmartUpload此jar包后 // 无法再次使用httpServletRequest这个对象来调用 // getParameter这个方法了 Request myreq = su.getRequest(); String name = myreq.getParameter("name"); String pass = myreq.getParameter("pass"); // 拿取所有上文件的对象 Files files = su.getFiles(); // 拿取我们上的唯一一个文件 // 0 表示索引值 File file = files.getFile(0); // 上文件的名字 String fileName = file.getFileName(); // 上文件的大小 int fileSize = file.getSize(); // 上文件的后缀名 String fileExt = file.getFileExt(); // 在服务器端开辟一个路径,建立文件夹放置文件 java.io.File myfile = new java.io.File(this.getServletContext() .getRealPath("/image")); // 如果不存在此路径 if (!myfile.exists()) { // 建立此路径 myfile.mkdir(); } // 58495849584954895.jpg String fileTrueName = new UUIDGenerator().generate() + "." + fileExt; // 组合一个另存为路径 // /image/43894834830430.jpg String finalPath = "/image/" + fileTrueName; // 另存为 file.saveAs(finalPath); Uploader up = new Uploader(); up.setName(name); up.setPass(pass); up.setPicPath(finalPath); UploaderDaoIf dao = new UploaderDaoImpl(); boolean flag = dao.addUp(up); if (flag) { this.getServletContext().setAttribute("up", up); response.sendRedirect("/JspDay3_upload/show.jsp"); } } catch (Exception ex) { ex.printStackTrace(); } } }
通过jsp前端进行多文件 显示层代码: <%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ page import="com.jspsmart.upload.*" %> My JSP 'index.jsp' starting page <script type="text/javascript"> function addFile(){ var myTB = document.getElementByIdx_x_x("myTB"); var rowNum = myTB.rows.length; var newRow = myTB.insertRow(rowNum); var cells_0 = newRow.insertCell(0); cells_0.innerHTML = "上文件:"; var cells_1 = newRow.insertCell(1); cells_1.innerHTML = ""; }
文件

============================================================================ 逻辑层代码: <%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ page import="com.jspsmart.upload.*" %> <% SmartUpload su = new SmartUpload(); su.initialize(pageContext); try{ su.setAllowedFilesList("jpg,jpeg,gif"); su.setDeniedFilesList("exe"); su.setMaxFileSize(1024*1024*1024*5); su.setTotalMaxFileSize(1024*1024*1024*20); su.setCharset("gbk"); su.upload(); }catch(Exception e){ out.print("您选择的文件非法或者长度超限,请检查!"); e.printStackTrace(); } for(int i=0;i文件上成功"); } } %>

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧