我在将文件写入系统中 tomcat 服务器的本地主机时遇到问题。
我如何在下面提到的位置写入文件?当文件上传到提到的位置时,我收到了回复。但我不知道它存储在我的系统中的什么位置。这是正确的做法吗??
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
String uploadedFileLocation = "http://127.0.0.1:8080/Attachments"
+ fileDetail.getFileName();
// save it
writeToFile(uploadedInputStream, uploadedFileLocation);
String output = "File uploaded to : " + uploadedFileLocation;
return Response.status(200).entity(output).build();
}
private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) {
try {
OutputStream out = new FileOutputStream(new File( uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
实际上我从http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/得到了这个 [writeToFile] 代码