SSH image upload and download

SSH image upload and download

Remember to modify the submit type of the form:multipart/form-data

two methods

  1. Use the dfs file system server method to upload to the project
  2. Convert the file or image to byte and save it to the database

save to project

  1. The page performs js code to determine the upload type
  2. Then get the suffix name of the uploaded file and submit it to the background
//获取一个上传文件的扩展名
var myfile = document.getElementById('myfile');
//获取上传文件的扩展名
var filevalue = myfile.value;
var index = filevalue.lastIndexOf('.');
//alert(filevalue.substring(index));
$("#imgType").val(filevalue.substring(index));
isExists = true;
return true;
  1. The action code saves the image to the project
  2. Use absolute paths, because it doesn't matter if it's a dfs system
private String imgType;
public String getImgType() {
    return imgType;
}
public void setImgType(String imgType) {
    this.imgType = imgType;
}
@Action(value = "productAction_insert")
public String insert() throws Exception {
    //获得属性驱动
    Product product = getModel();
    //获得request
    HttpServletRequest request = ServletActionContext.getRequest();
//        String productImage = request.getParameter("productImage");
    //转换成struts2的包装request
    MultiPartRequestWrapper mr = (MultiPartRequestWrapper) request;
    //获取request中的文件参数的数据
    File[] productImages = mr.getFiles("productImage");
    byte[] bytes = null;
    if (productImages != null && productImages.length > 0) {
        //file转成byte
        bytes = getBytesByFile(productImages[0]);
    }
    //把byte保存到数据库中
    product.setImageByte(bytes);

    String fieldValue = "/images/pro/"+product.getProductNo();
//        String name = productImages[0].getName();
//        String[] split = productImages[0].getName().split("\\.");
    //待会输出到项目文件夹的绝对路径
    String filg = "D:\\workspace\\ilcps-server\\ilcps-web\\src\\main\\webapp\\images\\pro\\"+product.getProductNo()+imgType;
    //以下代码完成上传文件
    OutputStream os = new FileOutputStream(new File(filg));
    //输出
    os.write(bytes);
    //关闭流
    IOUtils.closeQuietly(os);

    product.setProductImage(fieldValue+imgType);
    Factory factory2 = factoryService.findOne(getModel().getFactory().getId());
    if (factory2 != null) {
        product.setFactoryName(factory2.getFactoryName());
    }

    //3.1 调用service新增保存
    productService.saveOrupdate(product);
    //3.2保存成功,跳转到列表
    return "productList";
}

Use byte to save to database

  1. Modify the field of the database table to the blob type, so that the blob can save the byte of the file type
  2. Use hibernate to annotate the jpa modification of the entity class
  3. When using request to obtain, the request should be converted into a package request of struts2: MultiPartRequestWrapper
  4. Note that the size of the picture can be changed if it is too large
    • struts2 :
    • spring : 10240
    • You can see the above reference article
@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "IMAGE_BYTE", columnDefinition = "BLOB",nullable=true)
private byte[] imageByte;
  1. save to database
@Action(value = "productAction_insert")
public String insert() throws Exception {
    //获得属性驱动
    Product product = getModel();
    //获得request
    HttpServletRequest request = ServletActionContext.getRequest();
//        String productImage = request.getParameter("productImage");
    //转换成struts2的包装request
    MultiPartRequestWrapper mr = (MultiPartRequestWrapper) request;
    //获取request中的文件参数的数据
    File[] productImages = mr.getFiles("productImage");
    byte[] bytes = null;
    if (productImages != null && productImages.length > 0) {
        //file转成byte
        bytes = getBytesByFile(productImages[0]);
    }
    //把byte保存到数据库中
    product.setImageByte(bytes);
  • echo the image to the page
<img  src="${ctx }/pro/productAction_getImageByte?productId=${productId}" width='40px' height='30px'>
  • action
/**
 * 返回字节码图片
 */
@Action("productAction_getImageByte")
public void productAction_getImageByte(){
    HttpServletResponse response = ServletActionContext.getResponse();
    OutputStream sout = null;
    String productId = getModel().getProductId();
    Product product = productService.findOne(productId);
    byte[] bytes = product.getImageByte();
    try {
        response.setContentType("text/html");
        sout = response.getOutputStream();
        sout.write(bytes);
        sout.flush();
        sout.close();
        sout = null;
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325550675&siteId=291194637