我正在使用 Jersey API 来使用多部分数据(照片),并且我喜欢将其保存为服务器应用程序目录,而不是任何其他驱动器。我成功保存在 D 盘等任何其他驱动器中,但找不到将其保存在服务器应用程序目录中的方法。
@POST
@Path("uploadphoto")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("text/plain")
public String uploadNotices(@FormDataParam("file") InputStream uploadedInputStream) {
String uploadedFileLocation = "d:/1.jpg";
// save it
try {
writeToFile(uploadedInputStream, uploadedFileLocation);
} catch(Exception e) {
return "no";
}
return "yes";
}
// save uploaded file to new location
private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) throws Exception {
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();
}