NutzCN Logo
问答 图片地址上传到数据库的问题
发布于 1714天前 作者 wx_p064hjeoh9l2g0cqspro 1274 次浏览 复制 上一个帖子 下一个帖子
标签:

我现在要做的是添加新商品的功能。需要有图片的信息。但是我现在一传图片就报错:

java.lang.RuntimeException: org.nutz.mvc.upload.UploadException: Unknow Content-Type : application/x-www-form-urlencoded; charset=UTF-8

这是我的java后端代码:

@At
    @POST
    @AdaptBy(type=UploadAdaptor.class, args={"${app.root}/WEB-INF/tmp/user_avatar", "8192", "utf-8", "20000", "102400"})
    public Object add(@Param("file")TempFile tf,@Param("..") Product product) {
        NutMap nutMap = new NutMap();
        String msg = checkUser(product, true);
        String productBarcode = product.getProductBarcode();
        String numeric = isNumeric(productBarcode, true);//调用方法判断输入的产品编号是否正确
        if (numeric != null) {//如果用户输入的产品编号不合法。那么返回false
            return nutMap.setv("ok", false).setv("msg", numeric);
        }
        if (msg != null) {//根据返回的值来给前端进行反馈
            return nutMap.setv("ok", false).setv("msg", msg);
        }
        List<Product> queryList = dao.query(Product.class, null);
        if (queryList.size() == 0) {//如果数据库还没有值。也就是0的话。那么直接设置产品顺序是1
            product.setProductSort(1);
        }
        for (Product product1 : queryList) {
            Integer productSort = product1.getProductSort();//从数据库中取出产品顺序
            String barcode = product1.getProductBarcode();//从数据库取出产品编号
            if (barcode.equals(productBarcode)) {//进行判断。如果产品编号重复。返回false和错误信息
                return nutMap.setv("ok", false).setv("msg", "产品编号重复。请重新输入新的产品编号");
            }
            if (product.getProductName().equals(product1.getProductName())) {
                return nutMap.setv("ok", false).setv("msg", "产品名称重复。请重新输入");
            }
            if (productSort != null) {//这里说明已经存在商品顺序。取值加1
                product.setProductSort(productSort + 1);
            }
        }
        BufferedImage image = Images.read(tf.getFile());
        image = Images.zoomScale(image, 128, 128, Color.WHITE);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Images.writeJpeg(image, out, 0.8f);
        product.setProductImage(out.toByteArray());
        product.setCreateTime(new Date());//设置时间为当前时间
        product.setProductStatus("1");//默认状态为上架
        Product insert = dao.insert(product);//将数据插入到表中
        return nutMap.setv("ok", true).setv("data", insert);
    }

这是我的前端jsp代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>用户详情页</title>
    <script type="text/javascript"
            src="http://cdn.staticfile.org/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        // 添加方法
        function add() {
            $.ajax({
                type: "post",
                url: "http://localhost:8080/productModule/add",
                data: $("#productModule_add_form").serialize(),
                dataType: "json",
                success: function (data) {
                    if (data.ok) {
                        alert("添加成功");
                        location.reload();
                    } else {
                        alert(data.msg);
                    }
                }
            });
        }
        // 产品上架方法
        function productsOnShelves(productId) {
            $.ajax({
                type: "post",
                url: "http://localhost:8080/productModule/productsOnShelves?productId="+productId,
                dataType: "json",
                success: function (data) {
                    if (data.ok) {
                        alert("上架成功");
                        location.reload();
                    } else {
                        alert(data.msg);
                    }
                }
            });
        }
        // 产品下架方法
        function ProductUndershelf(productId) {
            $.ajax({
                type: "post",
                url: "http://localhost:8080/productModule/ProductUndershelf?productId="+productId,
                dataType: "json",
                success: function (data) {
                    if (data.ok) {
                        alert("下架成功");
                        location.reload();
                    } else {
                        alert(data.msg);
                    }
                }
            });
        }

        //查询方法
        var pageNumber = 1;
        var pageSize = 10;
        function query() {
            $.ajax({
                url : "http://localhost:8080/productModule/query",
                data : $("#productModule_query_form").serialize(),
                dataType : "json",
                success : function(data) {
                    console.log(data);
                    $("#product_count").html("共"+data.pager.recordCount+"个用户, 总计"+data.pager.pageCount+"页");
                    var list_html = "<tr>\n" +
                        "            <th>产品编号</th>\n" +
                        "            <th>产品名称</th>\n" +
                        "            <th>产品单价</th>\n" +
                        "            <th>产品规格</th>\n" +
                        "            <th>产品图片</th>\n" +
                        "            <th>产品顺序</th>\n" +
                        "            <th>产品状态</th>\n" +
                        "            <th>创建时间</th>\n" +
                        "            <th>修改</th>\n" +
                        "            <th>删除</th>\n" +
                        "            <th>产品上架</th>\n" +
                        "            <th>产品下架</th>\n" +
                        "        </tr>";
                    console.log(data.list);
                    for (var i=0;i<data.list.length;i++) {
                        var product = data.list[i];
                        console.log(product);
                        if (product.productStatus=="1"){
                            product.productStatus="已上架"
                        }else {
                            product.productStatus="未上架"
                        }
                        var tmp = "<tr>\n" +
                            "            <td>"+product.productBarcode+"</td>\n" +
                            "            <td>"+product.productName+"</td>\n" +
                            "            <td>"+product.productPrice+"</td>\n" +
                            "            <td>"+product.productStandard+"</td>\n" +
                            "            <td>"+product.productImage+"</td>\n" +
                            "            <td>"+product.productSort+"</td>\n" +
                            "            <td>"+product.productStatus+"</td>\n" +
                            "            <td>"+product.createTime+"</td>\n" +
                            "            <td><input type='button'><a href='http://localhost:8080/productModule/productById?productId="+product.productId+"'>修改</a></td>\n" +
                            "            <td><input type='button'><a href='http://localhost:8080/productModule/Remove?productId="+product.productId+"'>删除</a></td>\n" +
                            "            <td><input type='button'><button onclick='productsOnShelves("+product.productId+")'>产品上架</bntton></td>\n" +
                            "            <td><input type='button'><button onclick='ProductUndershelf("+product.productId+")'>产品下架</button></td>\n" +
                            "        </tr>";
                        list_html += tmp;
                    }
                    $("#abc").empty();
                    $("#abc").append(list_html);
                }
            });
        }
        $(function() {
            query();
        })

        // 'update.jsp?productId="+product.productId+"'






    </script>
</head>
<body>
<div>
    <form action="#" id="productModule_query_form">
        查询条件输入一个即可:产品名称:<input type="text" name="productName">产品状态:<input type="text" name="productStatus">创建时间:<input type="text" name="createTime">
        页数<input type="text" name="pageNumber" value="1">
        每页<input type="text" name="pageSize" value="10">
    </form>
    <button onclick="query()">查询</button>
    <p>---------------------------------------------------------------</p>
    <table id="abc" border="1" cellpadding="0" width="1500px">

    </table>
    <p id="product_count"></p>
    <div id="product_list">
    </div>
</div>


<div>
    <form action="#" id="productModule_add_form" method="post" enctype="multipart/form-data">
        产品编号<input name="productBarcode">
        产品名称<input name="productName">
        产品单价<input name="productPrice">
        产品规格<input name="productStandard">
        产品图片<input type="file" name="file">
        产品描述<textarea rows="3" cols="20" name="productDesc"></textarea>
    </form>
    <button onclick="add()">新增</button>
</div>
<div>

</div>

</body>
</html>

入门的文档我已经看了好几遍了。但是我不知道我到底哪块写错了。。。。入门的文档我做完了可以正常上传图片地址到数据库。但是我不知道我这为啥不行啊、。。。

1 回复

文件上传走ajax要第三方js库,例如ajaxfileupload,webupload

添加回复
请先登陆
回到顶部