java如何实现上传图片到服务器,并预览图片

java如何实现上传图片到服务器,并预览图片,第1张

预览,不能简单的用<img>,然后把地址赋给src的,会出现浏览器不兼容问题
用css滤镜,像下面
documentgetElementById("previewImg")stylefilter = "progid:DXImageTransformMicrosoftAlphaImageLoader(sizingMethod='scale',src='" + ovalue + "')";
至于如何上传,struts2很方便的

上传一般考虑两种办法:1、上传到服务器,数据库中存储服务器上的相对路径;2、转换为二进制流存入到数据库中。现在常用的做法都是选择第一种,因为第二种太占数据库空间,而且查找读取效率不高。很多开源的文件上传框架(比如spring中的commons-fileupload)都是采用第一种方式。上传到服务器不会影响系统的运行速度,你可以选择将的目录建立在非系统盘,存储容量比较大的盘,如F盘等。注意名字不要重复,建议用uuid

/
      等比例缩放
      @param infile
      @param outfile
      @param width
      @param height
      @param quality
      @throws IOException
      @throws InterruptedException
     /
    public static void Thumbnail(String infile, String outfile, int width, int height, int quality) throws IOException, InterruptedException {
        // save thumbnail image to OUTFILE
        //Systemoutprintln("infile:" + infile);
        BufferedImage thumbImage = null;
        BufferedOutputStream out = null;
        Image image = null;
        image = ToolkitgetDefaultToolkit()createImage(infile);
        MediaTracker mediaTracker = new MediaTracker(new Container());
        mediaTrackeraddImage(image, 0);
        mediaTrackerwaitForID(0);
        int thumbWidth = width;
        int thumbHeight = height;
        double thumbRatio = (double) thumbWidth / (double) thumbHeight;
        int imageWidth = imagegetWidth(null);
        int imageHeight = imagegetHeight(null);
        double imageRatio = (double) imageWidth / (double) imageHeight;
        if (thumbRatio < imageRatio) {
            thumbHeight = (int) (thumbWidth / imageRatio);
        } else {
            thumbWidth = (int) (thumbHeight  imageRatio);
        }
        thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImageTYPE_INT_RGB);
        Graphics2D graphics2D = thumbImagecreateGraphics();
        graphics2DsetRenderingHint(RenderingHintsKEY_INTERPOLATION, RenderingHintsVALUE_INTERPOLATION_BILINEAR);
        graphics2DdrawImage(image,0,0, thumbWidth, thumbHeight, null);
        out = new BufferedOutputStream(new FileOutputStream(outfile));
        JPEGImageEncoder encoder = JPEGCodeccreateJPEGEncoder(out);
        JPEGEncodeParam param = encodergetDefaultJPEGEncodeParam(thumbImage);
        quality = Mathmax(0, Mathmin(quality, 100));
        paramsetQuality((float) quality / 1000f, false);
        encodersetJPEGEncodeParam(param);
        encoderencode(thumbImage);
        outclose();
        thumbImage = null;
        out = null;
        image = null;
    }

原文转载自:>你做的是简单的上传?我这是spring的上传你可以用io流上传
public String picture(@RequestParam MultipartFile[] imgs,>

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/10531418.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-09
下一篇2023-05-09

发表评论

登录后才能评论

评论列表(0条)

    保存