来自计算引擎的CSV处理器弹簧启动

时间:2018-11-12 16:22:37

标签: java spring-boot

我正在尝试将带有主题ID,chapter_id,grade_id和csv文件的问题上传到服务器。所以我从这个控制器开始

@RestController
@RequestMapping("/api")
public class UploadingController
{

    @Autowired
    private UploadingService uploadingService;

    @RequestMapping(method = RequestMethod.POST, value = "/uploadBulkMcq/{answer_type}/{subject_id}/{chapter_id}/{grade_id}")
    public ReturnFormat uploadBulkMcq(@PathVariable long answer_type
            , @PathVariable long subject_id
            , @PathVariable long chapter_id
            , @PathVariable long grade_id
            , @RequestParam("files")  MultipartFile file )
    {
        return uploadingService.uploadBulkMCQ( answer_type, subject_id, chapter_id, grade_id, file );
    }

}

那么,为了创建服务类,我应该遵循哪种方法将问题上传到问题表中?请帮助我,我真的需要这个帮助。

1 个答案:

答案 0 :(得分:0)

首先我必须说我最近做了一些相同的事情:D 所以在这里您必须使用multipart,因为spring boot只支持multipart。

@Service
public class UploadingService
{

    @Autowired
    private QuestionRepository questionRepository;

    public ReturnFormat uploadBulkMCQ(long answer_type, long subject_id, long chapter_id, long grade_id, MultipartFile multipartFile)
    {
        ReturnFormat rf = new ReturnFormat();
        SuccessErrorList selist = new SuccessErrorList();
        try
        {
            File file = convertMultiPartToFile( multipartFile );

            try (Reader reader = new FileReader( file ))
            {
                CsvToBean<McqUploadingFormat> csvToBean = new CsvToBeanBuilder<McqUploadingFormat>( reader )
                        .withType( McqUploadingFormat.class )
                        .withIgnoreLeadingWhiteSpace( true )
                        .build();

                List<McqUploadingFormat> _questionList = csvToBean.parse();
                List<Question> questionList = new ArrayList<>();

                List<McqUploadingFormat> successList = new ArrayList<>();
                List<McqUploadingFormat> errorList = new ArrayList<>();

                for ( McqUploadingFormat _question : _questionList )
                {
                    Question question = new Question();
                    question.setChapterId( chapter_id );
                    question.setAnswerTypeId( answer_type );
                    question.setSubjectId( subject_id );
                    question.setCreatedBy( _question.getCreated_by() );
                    question.setDifficultyLevel( Integer.parseInt( _question.getDifficulty_level() ) );
                    question.setMultipleAnswers( _question.getMultiple_answers().equals( "1" ) );
                    question.setQuestion( _question.getQuestion() );

                    List<Answer> answers = new ArrayList<>();

                    int i;

                    for ( String a1 : Arrays.asList( _question.getAnswers().split( ";" ) ) )
                    {
                        Answer _answer = new Answer();
                        _answer.setAnswerQuestion( question );
                        i = 0;
                        for ( String a2 : Arrays.asList( a1.split( ":" ) ) )
                        {
                            if ( i == 0 )
                            {
                                _answer.setAnswer( a2 );
                            }
                            else
                            {
                                _answer.setCorrect( a2.equalsIgnoreCase( "Yes" ) || a2.equalsIgnoreCase( "1" ) );
                            }
                            i++;
                        }
                        answers.add( _answer );
                    }
                    question.setAnswers( answers );
                    questionList.add( question );
                    successList.add( _question );

                    //add validations for error list
                }

                try
                {
                    questionRepository.save( questionList );
                }
                catch ( Exception e )
                {
                    e.printStackTrace();
                }

                selist.setSuccessList( successList );
                selist.setErrorList( errorList );

                rf.setStatus( ReturnFormat.Status.SUCCESS );
                rf.setMessage( "Successfully inserted" );
                rf.setData( selist );

            }
        }
        catch ( Exception e )
        {
            rf.setStatus( ReturnFormat.Status.ERROR );
            rf.setMessage( "Exception Occured" );
            e.printStackTrace();
        }

        return rf;
    }

    private File convertMultiPartToFile( MultipartFile file ) throws IOException
    {
        File convFile = new File( file.getOriginalFilename() );
        FileOutputStream fos = new FileOutputStream( convFile );
        fos.write( file.getBytes() );
        fos.close();
        return convFile;
    }

    private void saveQuestion( Question question )
    {
        for ( Answer answer : question.getAnswers() )
        {
            answer.setAnswerQuestion( question );
        }
        questionRepository.save( question );
    }

}

这是我为您编辑的服务类别。和

public class ReturnFormat
{
    private Status status;
    private String message;
    private Object data;

    public ReturnFormat()
    {
    }

    public ReturnFormat( Status status, String message, Object data )
    {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public Status getStatus()
    {
        return status;
    }

    public void setStatus( Status status )
    {
        this.status = status;
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage( String message )
    {
        this.message = message;
    }

    public Object getData()
    {
        return data;
    }

    public void setData( Object data )
    {
        this.data = data;
    }

    public enum Status
    {
        SUCCESS, ERROR, CREATED, UPDATED, DELETED, FOUND, NOT_FOUND
    }

}