一只会飞的白菜 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
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮