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

    xiaoxiao2021-03-25  108

    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); }

    } `

    转载请注明原文地址: https://ju.6miu.com/read-19889.html

    最新回复(0)