将file类型的文件转换为blob,将blob类型转换为file

file类型需要转换为byte[],ibatis可以直接将byte[]类型存储进blob

`package com.yinhai.team.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionContext;
import com.yinhai.sysframework.dto.ParamDTO;
import com.yinhai.team.service.StaffTechTreeService;
import com.yinhai.webframework.BaseAction;

@Namespace(“/team”)
@Action(value = “uploadAction”, results = { @Result(name = “success”, location = “/kfzgl/team/upload.jsp”) })
public class UploadAction extends BaseAction {
private StaffTechTreeService staffTechTreeService = (StaffTechTreeService) getService(“staffTechTreeService”);
private File files;// 对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的
private String filesFileName;// 上传输入域FileName 文件名
private String filesContentType;// 上传文件的MIME类型

public String execute() {
    return SUCCESS;
}

/**
 * 上传文件
 * 
 * @return
 * @throws Exception
 */
public String testUplod() throws Exception {
    System.out.println(filesContentType);
    try {
        ServletContext sc = ServletActionContext.getServletContext();
        String storePath = sc.getRealPath("/files");
        FileUtils.copyFile(files, new File(storePath, filesFileName));
        ActionContext.getContext().put("message", "上传成功!");
    } catch (Exception e) {
        setMsg("下载失败", "error");
        e.printStackTrace();
    }
    FileInputStream fi = readImage(files);
    byte a[] = null;// **将测试文件test.doc读入此字节数组
    ParamDTO dto = getDto();
    dto.put("id", "14");
    int len = (int) files.length();
    a = new byte[len];
    int i = 0;
    int itotal = 0;
    // 将文件读入字节数组
    for (; itotal < len; itotal = i + itotal) {
        i = fi.read(a, itotal, len - itotal);
    }
    fi.close();
    dto.put("content", a);
    staffTechTreeService.insertTest(dto);
    return JSON;
}

public File getFiles() {
    return files;
}

public void setFiles(File files) {
    this.files = files;
}

public String getFilesFileName() {
    return filesFileName;
}

public void setFilesFileName(String filesFileName) {
    this.filesFileName = filesFileName;
}

public String getFilesContentType() {
    return filesContentType;
}

public void setFilesContentType(String filesContentType) {
    this.filesContentType = filesContentType;
}

/**
 * 得到blob文件
 * 
 * @return
 * @throws IOException
 * @throws SQLException
 */
@SuppressWarnings("unchecked")
public String getBlob() throws IOException, SQLException {
    ParamDTO dto = getDto();
    dto.put("id", "14");
    List<Map<String, Object>> list = getDao().queryForList("stafftechtree.ghlx_document_sel", dto);
    Blob blob = (Blob) list.get(0).get("content");
    OutputStream op = new FileOutputStream(new File("d:\\song\\a.jpg"));
    op.write(blob.getBytes(1, (int) blob.length()));
    op.close();
    return JSON;
}

/**
 * 将文件转换为二进制流
 * 
 * @throws FileNotFoundException
 */
public FileInputStream readImage(File file) throws FileNotFoundException {
    return new FileInputStream(file);
}

}
`

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值