一只会飞的白菜 2017-04-13 14:20 采纳率: 0%
浏览 1289

在jsp中如何有file上传文件到oracle数据库中去代码如下

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.entity.Goods"%>
<%@page import="com.biz.impl.GoodsBizimpl"%>
<%@page import="com.jspsmart.upload.SmartUpload"%>
<%@page import="com.jspsmart.upload.Files"%>
<%

//创建一个上传的组件
SmartUpload su = new SmartUpload();
//初始化SmartUpload对象(重)
su.initialize(pageContext); //pageContext上下文 -- application

com.jspsmart.upload.File file = null;  //非IO包下的File类




com.jspsmart.upload.Request req  = null;
//定义允许上传类型
String allowed = "gif,jpg,png,txt";
//定义不许上传类型
String denied = "jsp,asp,php,aspx,html,htm,exe,bat";
//文件上传类型,二选一,一般会选择允许上传类型
//定义上传文件大小
int file_size = 5*1024*1024;  //不能超过2M
String exceptionMsg = null;  //异常信息对象
try {

        //定义允许上传文件类型   
        su.setAllowedFilesList(allowed);
        //不允许上传文件类型   
        su.setDeniedFilesList(denied);      
        //单个文件最大限制   
        su.setMaxFileSize(file_size);   
        //设置上传对象的编码                   
        su.setCharset("utf-8");  //GBK   GB2312
        //执行上传(重)
        su.upload();
        String filepath = null; //路径


        //su.getFiles()获得所有文件对象
        Files f= su.getFiles();
        //循环读取文件对象
        for(int i=0; i<f.getCount(); i++){
            //取文件对象
            file=f.getFile(i);
            //判断是否有对象
            if(!file.isMissing()){
                //设置文件在服务器的保存位置
                filepath = "upload\\";  //保存到一个upload的文件夹下
                //filepath += file.getFileName();  //拼接文件名字
                filepath += System.currentTimeMillis()+ file.getFileName().substring( file.getFileName().lastIndexOf("."));
                //文件另存为   
                file.setCharset("utf-8");
                //保存(重)
                file.saveAs(filepath, SmartUpload.SAVE_VIRTUAL);  //上传模式
            }
        }

    } catch (Exception e) {
        exceptionMsg = e.getMessage();
        e.printStackTrace();
    }

//转码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");

//获取传过来的i的值

String productName=request.getParameter("productName");
int parentId=Integer.parseInt(request.getParameter("parentId"));
String photo=request.getParameter("photo");
int jiage=Integer.parseInt( request.getParameter("jiage"));
int kuchun=Integer.parseInt( request.getParameter("kuchun"));
String tiaoma=request.getParameter("tiaoma");

//out.print(productName+" "+ parentId+" "+ photo+" "+ jiage +" "+kuchun +" "+tiaoma);

//调dao 方法
Goods g=new Goods(productName,parentId,photo,jiage,kuchun,tiaoma);
int n=new GoodsBizimpl().getCr(g);

  if(n>0){
    out.print("添加成功");
  }else{
    out.print("添加失败");
   }

%>

获取那个值不会,一直报错

  • 写回答

2条回答 默认 最新

  • 寻梦csdn 2017-04-15 02:52
    关注

    String photo=request.getParameter("photo"); 这个是文件类型的吗?,是的话去掉。
    界面表单为

    需要属性 enctype="multipart/form-data"
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记