一只会飞的白菜 2017-04-16 10:21 采纳率: 0%
浏览 768
已结题

传张图片到服务期中,存入orcale数据库中去,在取出来,下面有一些代码,

如何用组件传图片到数据库和服务器中去,然后在从数据库中把这张图片取值到jsp页面中去但有可能不正确急急急就差他了

<%@ 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("添加失败");
   }

%>

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

  • 写回答

5条回答 默认 最新

  • 犊子哥哥 2017-04-16 11:43
    关注

    关系型数据库一般不选择保存文件,选择保存路径的,你把图片路径保存好,再把路径读取出来然后用路径找文件就是了

    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式