我想上传一个CSV文件,然后从中获取详细信息以填满MySQL数据库。我正在使用Play!框架2与Java,但我很难实现这个解决方案。
这是我的Java代码
public static Result uploadfile() {
File fl = request().body().asRaw().asFile();
if (fl != null) {
return ok("File uploaded");
} else {
flash("error", "Missing file");
return redirect(routes.Application.index());
}
这是我的Upload.Scala.html代码
@(uploadForm: Form[Application.upl])
@import helper._
@main("Upload file") {
@form(action = routes.Application.uploadfile) {
<input type="file" name="flname">
<p>
<input type="submit" value="upload">
</p>
}
}
最后我的路线文件
POST /uploadfile controllers.Application.uploadfile()
一旦我点击上传,就会发生执行异常,说明request().body().asRaw().asFile()
实际上是null
!
[NullPointerException:null]
任何想法我做错了什么?
=============================================== ==================================== 好的,我现在的代码如下 在application.java
上 public static Result uploadfile() {
play.mvc.Http.MultipartFormData body = request().body().asMultipartFormData();
FilePart flpart = body.getFile("fl") ;
if (flpart != null) {
File file = flpart.getFile();
ImportCSV impCSV = new ImportCSV();
try{
if (file.exists())
{
BufferedReader br = new BufferedReader( new FileReader(file));
System.out.println(file.getName() + " - " + file.getAbsolutePath());
impCSV.importCSV(br);
}
else
{
System.out.println("File not found!");
return redirect(routes.Application.upload());
}
}
catch(Exception e)
{
System.err.println("Buffering file " + file.getName() +": " + e.getMessage());
}
return ok("File uploaded");
} else {
flash("error", "Missing file");
return redirect(routes.Application.index());
}
}
在ImportCSV.java(扩展模型)上
public static void importCSV( BufferedReader br)
{
parseFile(br);
if(vals!=null)
{
Student student = new Student();
List<Student> newStudents;
newStudents = GetStudentDetails(vals);
try{
student.insertStudentList(newStudents);
}
catch(Exception e)
{
System.err.println("student inserting Error: " + e.getMessage());
}
}
}
static void parseFile( BufferedReader br) {
try {
String line = "";
StringTokenizer token = null;
int lineNum = 0, tokenNum = 0;
List<values> CSVvalues = new LinkedList<values>();
values LnValues = new values();
while((line = br.readLine()) != null) {
lineNum++;
// break comma separated file line by line
token = new StringTokenizer(line, ",");
LnValues = new values();
while(token.hasMoreTokens()) {
tokenNum++;
switch(tokenNum)
{
//Student ID
case 0:
{
LnValues.setStudID(Long.getLong(token.nextToken()));
}
//Student Name
case 1:
{
LnValues.setStudName(token.nextToken());
}
//Student DoB
case 2:
{
LnValues.setStudDoB(Date.valueOf(token.nextToken()));
}
//Class
case 3:
{
LnValues.setCl(token.nextToken());
}
//Year
case 4:
{
LnValues.setYear(Integer.parseInt(token.nextToken()));
}
//Quarter
case 5:
{
LnValues.setQuarter(token.nextToken());
}
//Maths Grade
case 6:
{
LnValues.setGradeM( Float.parseFloat(token.nextToken()));
}
//Computer Science Grade
case 7:
{
LnValues.setGradeCS(Float.parseFloat(token.nextToken()));
}
//Literature grade
case 8:
{
LnValues.setGradeL( Float.parseFloat(token.nextToken()));
}
}
}
CSVvalues.add(LnValues);
tokenNum = 0;
}
vals = CSVvalues;
} catch(Exception e) {
System.err.println("Parsing file Error: " + e.getMessage());
}
一旦我通过Buffered阅读器,数据就为空。一开始我试图将文件(文件fl)发送到参数但我也得到null错误。我尝试在某些情况下显示的@Util注释,但编译器抱怨没有识别它! 我愿意接受建议!
答案 0 :(得分:3)
这里有关于如何执行此操作的说明: http://www.playframework.org/documentation/2.0/JavaFileUpload
request().body().asRaw().asFile();
仅在您使用ajax上传文件时有效,如果您使用的是普通POST
请求,则应使用
public static Result upload() {
MultipartFormData body = request().body().asMultipartFormData();
FilePart picture = body.getFile("picture");
if (picture != null) {
String fileName = picture.getFilename();
String contentType = picture.getContentType();
File file = picture.getFile();
return ok("File uploaded");
} else {
flash("error", "Missing file");
return redirect(routes.Application.index());
}
}
另请注意,您必须使用正确的表单格式:
@form(action = routes.Application.upload, 'enctype -> "multipart/form-data")