フロントからJava(Spring Framework)でCSVファイルを受け取る事例です。
HTML
<form id="fileuploadform">
<input id="file" name="file" type="file">
<button id="upload" type="submit">アップロード</button>
</form>
|
Javascript
$("#upload").click(function() {
var formData = new FormData(
$('#fileuploadform').get()[0]
);
$.ajax({
method: 'post',
contentType: false,
processData: false,
data: formData,
url: '/post',
}).done(function(data) {
...
});
}
);
|
JAVA
@RequestMapping(value = "/post", method = RequestMethod.POST)
public ModelAndView post(
HttpServletResponse response, @RequestParam("file") MultipartFile file){
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferReader = null;
try {
inputStream = file.getInputStream();
inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
bufferReader = new BufferedReader(inputStreamReader);
while ((line = bufferReader.readLine()) != null) {
byte[] b = line.getBytes();
line = new String(b, "UTF-8");
String[] columns = line.split(",",-1);
for (int i = 0; i < columns.length; i++) {
if(i == 0) {
String colA = columns[i];
} else if(i == 1) {
String colB = columns[i];
}
}
}
...
|