javaweb上传图片(javaweb上传图片到数据库保存)

华为云服务器特价优惠火热进行中!

2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。

合作流程:
1、点击链接注册/关联华为云账号:点击跳转
2、添加客服微信号:cloud7591,确定产品方案、价格方案、服务支持方案等;
3、客服协助购买,并拉微信技术服务群,享受一对一免费技术支持服务;
技术专家在金蝶、华为、腾讯原厂有多年工作经验,并已从事云计算服务8年,可对域名、备案、网站搭建、系统部署、AI人工智能、云资源规划等上云常见问题提供更专业靠谱的服务,对相应产品提供更优惠的报价和方案,欢迎咨询。

今天给各位分享javaweb上传图片的知识,其中也会对javaweb上传图片到数据库保存进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

微信号:cloud7591
如需了解更多,欢迎添加客服微信咨询。
复制微信号

本文目录一览:

java web开发,上传图片并读取

java web开发中,使用文件操作类来上传图片并读取,如下代码:

 * @desc: 图片处理工具

 * @author: bingye

 * @createTime: 2015-3-17 下午04:25:32

 * @version: v1.0

 */

public class ImageUtil {

 

    /**

    * 将图片写到客户端

    * @author: bingye

    * @createTime: 2015-3-17 下午04:36:04

    * @history:

    * @param image

    * @param response void

    */

    public static void writeImage(byte[] image,HttpServletResponse response){

        if(image==null){

            return;

        }

        byte[] buffer=new byte[1024];

        InputStream is=null;

        OutputStream os=null;

        try {

            is=new ByteArrayInputStream(image);

            os=response.getOutputStream();

            while(is.read(buffer)!=-1){

                os.write(buffer);

                os.flush();

            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally{

            try {

                if(is!=null){is.close();}

                if(os!=null){os.close();}

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

     

    /**

    * 获取指定路劲图片

    * @author: bingye

    * @createTime: 2015-3-21 上午10:50:44

    * @param filePath

    * @param response void

    */

    public static void writeImage(String filePath,HttpServletResponse response){

        File imageFile=new File(filePath); 

        if(imageFile!=null  imageFile.exists()){

            byte[] buffer=new byte[1024];

            InputStream is=null;

            OutputStream os=null;

            try {

                is=new FileInputStream(imageFile);

                os=response.getOutputStream();

                while(is.read(buffer)!=-1){

     腊散郑     轮颂          os.write(buffer);

                    os.flush();

                }

            } catch (FileNotFoundException e) {

                e.printStackTrace();

            } catch (IOException e) {

                e.printStackTrace();

            } finally{

                try {

                    if(is!=null){is.close();}

                    if(os!=null){os.close();}

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

        }

    }

     

    /**

    * 图片上传到文件夹

    * @author: bingye

    * @createTime: 2015-3-20 下午08:07:25

    * @param file

    * @param savePath

    * @return boolean

    */

    public static ResultDto uploadToLocal(CommonsMultipartFile file,String savePath){

        if(file!=null  !file.isEmpty()){

            //获取文件名称

            String fileName=file.getOriginalFilename();

            //获取后缀名

            String suffixName=fileName.substring(fileName.indexOf(".")+1);

            //新名称

            String newFileName=System.currentTimeMillis()+"."+suffixName;

            //新文件路劲

            String filePath=savePath+newFileName;

            //获取存储文件路径

            File fileDir=new File(savePath);

         掘孝   if(!fileDir.exists()){

                //如果文件夹没有:新建

                fileDir.mkdirs();

            }

            FileOutputStream fos=null;

            try {

                fos=new FileOutputStream(filePath);

                fos.write(file.getBytes());

                fos.flush();

                return ResultUtil.success("UPLOAD_SUCCESS", URLEncoder.encode(newFileName,"utf-8"));

            } catch (Exception e) {

                e.printStackTrace();

                return ResultUtil.fail("UPLOAD_ERROR");

            } finally{

                try {

                    if(fos!=null){

                        fos.close();

                    }

                } catch (IOException e) {

                    e.printStackTrace();

                    return ResultUtil.fail("UPLOAD_ERROR");

                }

            }

        }

        return ResultUtil.fail("UPLOAD_ERROR");

    }

     

     

     

}

java web项目上传一些图片,必须刷新文件夹才能显示图片,如何编写自动刷新代码??

图片不会自动显示是不是浏览器缓存问题~?就是譬如两次请求都是同一个地址,但是如果你存储的图片已经更改,但是名字没变,这样浏览器貌似是不会重新发出请举尘销求。你兄颤可以在请求时候附加一个随机数,

或者你正游可以再详细描述下,你说的文件夹是在哪里的文件夹,网络上的?~还是本地?你说显示图片是在哪里显示?页面上?还是本地文件夹里面?

java web怎么上传图片到指定的项目文件夹,并获取该路径,将路径存储到数据库,用相对路径哦

当前项目的路经,通过request.getSession().getServletContext().getRealPath("/"世禅)获取此碰到搜扒尘的

用Java Web的jsp制作图片上传和显示如何实现

用jspSmartUpload组件亏友来实现,用jsp+servlet在Servlet里实现唤凯的代码:

PrintWriter out = response.getWriter();

int count = 0;

// 实例化上传控件对象

SmartUpload su = new SmartUpload();

// 初始化操作

su.initialize(config, request, response);

// 设置上传文件最大字节数

su.setTotalMaxFileSize(100000);

//

try {

//禁止上传指定扩展名的文件

su.setDeniedFilesList("ext,bat,jsp");

} catch (SQLException e1) {

e1.printStackTrace();

}

try {

// 上传文件到服务器

su.upload();

File fileup = new File(request.getRealPath("upload"));

if(!fileup.exists()){

// 创建目录

fileup.mkdir();

}

// 处理多个文件的上传

for(int i = 0;i su.getFiles().getCount();i++){

com.jspsmart.upload.File file = su.getFiles().getFile(i);

if(!file.isMissing()){ // 如果文件有效

// 保存文件到指定上传目录

file.saveAs("/upload/new."+file.getFileExt(), su.SAVE_VIRTUAL);

count = su.save("/upload");

}

}

} catch (SmartUploadException e) {

e.printStackTrace();

}

out.println(count +"file(s) uploaded");

如果你对这个上传组件不了解,最好是先去查查和空唤用法。。。

javaweb上传图片的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于javaweb上传图片到数据库保存、javaweb上传图片的信息别忘了在本站进行查找喔。

发布于 2023-04-13 02:04:32
收藏
分享
海报
30
目录

    忘记密码?

    图形验证码

    复制成功
    微信号: cloud7591
    如需了解更多,欢迎添加客服微信咨询。
    我知道了