서버에서 업로드 작업을 수행 할 수 없습니다. , 다음이 보이지 않는다 "당신이 플레이 할 수있는 다섯 가지 멋진 것들 중 하나"반대로 작동합니다 :기본 파일 업로드가 실패합니다.
:이보기와@Check("registered")
private static String doFileUpload(InputStream is, Score score) throws IOException {
//get Score from db
//get dir if present
java.io.File dir = new java.io.File("/public/uploads/" + play.templates.JavaExtensions.slugify(score.title));
//if not, create
if (!dir.exists()) {
dir.mkdirs(); //create new dir if not present
}
//create file on server
java.io.File newfile = new java.io.File(dir, "testfile.txt");
OutputStream os = new FileOutputStream(newfile);
IOUtils.copy(is, os);
return newfile.getAbsolutePath();
}
:
public static void doSingleFileUpload(
Long id, @Required java.io.File upload, String description, String title
) {
Score score = Score.findById(id);
try {
score.files.add(new File(doFileUpload(new FileInputStream(upload), score), title));//PLAY crashes here, with a nullpointer exception on the upload parameter
} catch (IOException ex) {
//TODO: do something nice
}
score.save();
}
doFileUpload
<!-- ... -->
<form action="@{ScoreController.doSingleFileUpload()}" method="POST" enctype="multipart/form-data">
<input type="hidden" name="id" value="${score.id}" />
<input type="file" id="upload" name="upload" />
<input type="text" name="description" />
<input type="hidden" name="title" value="${score.title}" />
<input type="submit" value="submit" />
</form>
왜 upload
은 null입니까? 비슷한 문제가 발생했습니다. here. 하지만 POST 후 헤더를 보면, 거기에 정보가 거의 없다는 것입니다.
내가 뭘 잘못하고 있니?
FF4로 테스트 중이며 Play 1.1.1을 사용 중입니다.
편집 : 이 샘플 응용 프로그램은 1.1.1 및 1.2에서 모두 작동합니다.
컨트롤러 :
public class Application extends Controller {
public static void index() {
File dir = new File(Play.applicationPath+File.separator+"public"+File.separator+"uploads");
if (!dir.exists() && dir.isDirectory()) {
renderText("something went wrong");
} else {
String[] files = dir.list();
if (files != null) {
render(dir);
} else {
render();
}
}
}
public static void upload(File upload) throws FileNotFoundException, IOException {
File dir = new File(Play.applicationPath+File.separator+"public"+File.separator+"uploads");
if (!dir.exists()) {
dir.mkdirs();
}
File newfile = new File(dir, upload.getName());
FileInputStream fis = new FileInputStream(upload);
FileOutputStream fos = new FileOutputStream(newfile);
IOUtils.copy(fis, fos);
index();
}
}
보기 :
#{extends 'main.html' /}
#{set title:'Home' /}
#{form @Application.upload(), enctype:'multipart/form-data'}
<input type="file" name="upload" />
<input type="submit" />
#{/form}
#{if (dir.list()!=null)}
<ul>
#{list items:dir.list(), as:'file'}
<li><a href="public/uploads/${file}">${file}</a></li>
#{/list}
</ul>
#{/if}
이제 질문은 다음과 같습니다 응용 프로그램 사이의 차이는 첫 번째가 더 많이 가지고 있다는 사실을 제외하고는 무엇입니까 매개 변수 ...
모두에게 도움을 주셔서 감사합니다!
인사, 재스퍼
어떤 Play 버전을 사용하고 있으며 doFileUpload의 코드는 무엇입니까? 공개 정적 인 경우 코드가 작동하지 않습니다. – Codemwnci
답변 해 주셔서 감사합니다. 귀하의 답변에 대한 답변으로 내 게시물을 편집했습니다 ... 공개 방법은 아니지만 '업로드'에서 'null'값을 설명하지 않습니까? – Jasper