【问题标题】:How to display byte[] to the UI in string of words如何以字符串形式向 UI 显示字节 []
【发布时间】:2015-11-03 04:56:34
【问题描述】:

我正在从 ms 字中输入字符串并使用 blob 保存在数据库中,我现在想从数据库中获取数据并将其显示到 UI。请帮助我如何将内容呈现到 UI

这是我的控制器,用于展示我如何从 MS Word 中获取数据:

@RequestMapping(value="studentattedanceupload", method = RequestMethod.POST)
public @ResponseBody String AttendanceUpload(MultipartHttpServletRequest request, HttpServletResponse response, @RequestParam Long batchId, @RequestParam Long slotId, @RequestParam String date) {
    String langCode = "EN";
    Long moduleId = (long) 1;

    try {
        MultipartFile multipartFile = request.getFile("filename");
        byte[] bytes = multipartFile.getBytes();
        String content= multipartFile.getContentType();

        mockTestService.saveInstruction(bytes,content, langCode, moduleId);

        return "1";
    } catch (Exception e) {
        logger.error("Error while uploading the Attendance List", e);
    }
    return "2";
}

这是我的服务层:

@Transactional(propagation=Propagation.REQUIRED)    
public void saveInstruction(byte[] bytes,String content, String langCode, Long moduleId) throws FileNotFoundException {
    // TODO Auto-generated method stub
    ModuleDetails moduleDetails = moduleDetailsDao.getModuleDetailsByMid(moduleId);
    if(moduleDetails != null){
        LanguageDetails languageDetails = languageDetailsDao.getLanguageDetailsByLangId(langCode);
        if(languageDetails != null){

            InstructionSet instructionSet = instructionSetDao.getInstructionSetById(new InstructionSetPK(moduleDetails, languageDetails));
            if(instructionSet == null){
                instructionSet = new InstructionSet();
                instructionSet.setInstructionSetPK(new InstructionSetPK(moduleDetails, languageDetails));
                instructionSet.setInstruction(bytes);
                instructionSet.setContent(content);
                instructionSetDao.save(instructionSet);
            }else{
                instructionSet.setInstruction(bytes);
            }
        }else{
            throw new FileNotFoundException();
        }
    }else{
        throw new FileNotFoundException();
    }
}

这是我的 DAO:

@Override
public Serializable save(Object entity) {
    // TODO Auto-generated method stub
    return  currentSession().save(entity);
}

这是我的模型: InstructionSet模特

@Entity
@Table(name="TB_INSTRUCTION_SET")
public class InstructionSet implements Serializable {

    @EmbeddedId
    private InstructionSetPK instructionSetPK;

    @Column(name="INSTRUCTION")
    @Lob
    private byte[] instruction;

    @Column(name="CONTENT")
    private String content;

    public InstructionSet() {
        // TODO Auto-generated constructor stub
    }

// getter 和 setter 被初始化

InstructionSetPk 的型号

@Embeddable
public class InstructionSetPK implements Serializable {

    @ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    @JoinColumn(name="MODULE_ID")
    private ModuleDetails moduleDetails;

    @ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    @JoinColumn(name="LANG_ID")
    private LanguageDetails languageDetails;

    public InstructionSetPK() {
        // TODO Auto-generated constructor stub
    }

现在我希望将存储在数据库中的数据作为在 Word 中输入的字符串显示在 UI 中。为此,我能够获得byte[],但我想以 ms 字而不是加密的byte 显示内容。

这是我试图获取数据的服务层:

@Override
public InstructionSet getInstructionFromModuleIdAndLangCode(Long moduleId,
            String langCode) {
    ModuleDetails moduleDetails = moduleDetailsDao.getModuleDetailsByMid(moduleId);
    if(moduleDetails != null){
        LanguageDetails languageDetails = languageDetailsDao.getLanguageDetailsByLangId(langCode);
        if(languageDetails != null){
            InstructionSet instructionSet = instructionSetDao.getInstructionSetById(new InstructionSetPK(moduleDetails, languageDetails));
            return instructionSet;
        }
    }
    return null;
}

我的控制器(我卡在那里

@RequestMapping(value="instructiondisplay", method = RequestMethod.GET)
public void instructionDisplay(HttpServletRequest request , HttpServletResponse response/*, @RequestParam String langCode, @RequestParam Long moduleId*/){
    String langCode = "EN";
    Long moduleId = (long) 1;
    try{
        InstructionSet instructionSet=mockTestService.getInstructionFromModuleIdAndLangCode(moduleId, langCode);
        if(instructionSet!=null){
            InputStream inputStream= request.getInputStream();
            inputStream.read(instructionSet.getInstruction());
        }
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return ;    
}

我不确定如何获取数据并将其发送到 UI。

【问题讨论】:

  • 我能够以 byte[] 格式发送 UI 的数据,但我只需要发送字符串...这就是我卡住的地方,我不确定如何使用发送字符串输入流
  • 您在寻找new String(byte[])吗? (并且不应该有InputStream 参与)
  • 我尝试使用 String(byte[]) 但我仍然得到一些加密的数据。我已经从 MS Word 中发送了“Hi world”,并将其作为 blob 类型保存在数据库中。不,我想检索 Hi 世界并将其作为字符串发送到 UI,而不是现在我得到一些字节类型的加密数据....
  • getBytes 在文件上返回文件内容,MSWord 不是纯文本,所以文件内容不是文本内容。

标签: java hibernate spring-mvc blob inputstream


【解决方案1】:

这里可以使用Apache POI,已经用于阅读docx、xls等。

【讨论】:

  • 不,我在 db 中有数据,数据类型是 blob。我需要的只是获取数据并在输入流的帮助下显示它。那是我卡住了
  • 正如您所提到的,您可以使用输入流,使用该输入流创建文件或文件输入流对象,然后将此文件作为参数发送给其他对象。如果我越界,请忽略。
猜你喜欢
  • 2018-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 2012-01-06
  • 1970-01-01
相关资源
最近更新 更多