文件上传方式

来源:互联网 发布:双拼域名注册大师 编辑:程序博客网 时间:2024/05/29 09:06

1.文件的上传(sqlite文本数据库)

页面:<tr height="30" bgcolor="#F1F1F1">
          <td width="128" class="tb11">图片</td>
          <td class="tb12"><input type="file" name="img">
            <span id="note-devicename">&nbsp;</span></td>
        </tr>

后台中操作

if (img == null) {

            throw new RuntimeException("请上传图片!");//判断数据是否上传的值
        }
        if (img.indexOf(".") == -1) {
            throw new RuntimeException("您上传的文件无法获得后缀名!");//判断数据上传的值中是否拥有
        }
        String temp = img.substring(img.indexOf(".") + 1, img.length());//获取文件的后缀名
        if (!temp.equals("bmp") && !temp.equals("dib") && !temp.equals("gif")//判断后缀名的格式
                && !temp.equals("jpe") && !temp.equals("jpeg")
                && !temp.equals("jpg") && !temp.equals("png")
                && !temp.equals("ico")) {
            throw new RuntimeException(
                    "您上传的图片格式不符合要求,请确定上传的是bmp/gif/jpg/ico/png/jpeg格式!");
        }
        File file = new File(getFiles().get("img").toString());//获取文件
        if (!file.exists()) {
            throw new RuntimeException("请上传图片!");
        }
        String newImg = "img/" + UUID.randomUUID();//给其一个随机的名称
        FileUtils.copyFile(file, new File(Http.baseUrl + newImg));//public class Http extends NanoHTTPD {public static String baseUrl = "sdcard/web/";文件的copy的文件
        area.setImg(newImg);//将文件的新名称导入到数据库中
        AreaControl.areaDao.insert(area);
原创粉丝点击