我只想使用 jersey rest 服务和 Jquery ajax 作为客户端上传文件这是我的代码 1. HTML
<form action="rest/file/upload" method="post" enctype="multipart/form-data">
<p>
Select a file : <input type="file" name="file" />
</p>
<input type="submit" value="Upload It" />
</form>
2.休息服务
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("file") InputStream stream) {
String uploadedFileLocation = "E:\\\\uploaded\\test.jpg";
//Session s = Session.getDefaultInstance(new Properties());
//InputStream is = new ByteArrayInputStream(<< String to parse >>);
//MimeMessage message = new MimeMessage(s, stream);
//Multipart multipart = (Multipart) message.getContent();
// save it
writeToFile(stream, uploadedFileLocation);
String output = "File uploaded to : " + uploadedFileLocation;
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
return Response.status(200).entity(output).build();
}
// save uploaded file to new location
private void writeToFile(InputStream uploadedInputStream,
String uploadedFileLocation) {
try {
byte[] image = IOUtils.toByteArray(uploadedInputStream);
OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
IOUtils.write(image, out);
/*int read = 0;
byte[] bytes = new byte[1024];
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}*/
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
它的工作但流也包括这条线
-----------------------------7dd3b827a0ddc
内容处置:表单数据;名称=“文件”;filename="Jellyfish.jpg" 内容类型:image/pjpeg
如何从输入流中删除它?需要专业解答