0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CSVをOrangeSignal CSVを使ってパースする

Last updated at Posted at 2016-02-14

CSVをOrangeSignal CSVを使ってパースする。

public class UploadAction {
	@Resource
	@ActionForm
	public UploadForm uploadForm;

	@Required
	@Binding(bindingType = BindingType.NONE)
	public FormFile formFile;

	@Binding(bindingType = BindingType.NONE)
    public FormFile[] formFiles;

	public HttpServletRequest request;
	public List<String[]> csvData;
	@Resource
	public MSongService mSongService;
	@Resource
	public TMarkingService tMarkingService;


	public MSong mSong;
	public TMarking tMarking;

	@Execute(validator=false)
	public String index(){
		return "index.jsp";
	}
	@Execute(validator=false)
	public String upload() throws ParseException, FileNotFoundException, IOException{
		CsvConfig cfg = new CsvConfig();
		cfg.setQuoteDisabled(false);// デフォルトでは無効となっている囲み文字を有効にします。
		cfg.setEscapeDisabled(false);// デフォルトでは無効となっているエスケープ文字を有効にします。
		cfg.setIgnoreLeadingWhitespaces(true);// 項目値前のホワイトスペースを除去します。
		cfg.setIgnoreTrailingWhitespaces(true);// 項目値後のホワイトスペースを除去します。
		cfg.setIgnoreEmptyLines(true);// 空行を無視するようにします。
		InputStream is = uploadForm.file.getInputStream();
		File file = new File("/", uploadForm.file.getFileName());
		try {
		    //ISから一時ファイル作成
			Files.copy(is, file.toPath(),StandardCopyOption.REPLACE_EXISTING);
		} catch (IOException e) {
		    e.printStackTrace();
		}
		try {
			csvData = Csv.load(new File(file.getAbsolutePath()),"MS932", cfg, new StringArrayListHandler());
			//一時ファイル削除
			Files.deleteIfExists(file.toPath());
		} catch (IOException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}
		System.out.println(file.getAbsolutePath());


		for(int i=1;i<csvData.size();i++){
			String[] str=csvData.get(i);
			//Songマスタにinsert
			mSong=mSongService.findById(str[REQUEST_NO]);
			if(mSong==null){
				mSong=new MSong();
				mSong.requestNo=str[REQUEST_NO];
				mSong.artist=str[ARTIST];
				mSong.songTitle=str[SONG_TITLE];
				mSong.highTess=Integer.parseInt(str[HIGH_TESS]);
				mSong.lowTess=Integer.parseInt(str[LOW_TESS]);
				mSongService.insert(mSong);
			}
			//markingテーブルへのinsert
			Timestamp dateTime = new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str[DATETIME]).getTime());		
			
		ActionMessages errors = new ActionMessages();
		errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("errors.upload.complete"));
		ActionMessagesUtil.addMessages(request, errors);
		return "index.jsp";
	}
}
public List<String[]> csvData;
//iは行数
String[] str=csvData.get(i);
//添え字は横方向のセル
str[REQUEST_NO];

OrangeSignal CSV - ユーザーガイド

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?