0
我想上传一个图像到服务器使用Angular作为前端和java ee web服务jax rs rest作为我的后端很容易。这是我对角/前端代码:Angular 4文件上传与休息容易jax rs java ee
HTML页面:
<md-card>
<input type="file" (change)="onChange($event)" placeholder="Upload
files" >
</md-card>
对于组件:
fileChange(event) {
let fileList: FileList = event.target.files;
let fileListLength = fileList.length;
if(fileListLength > 0) {
let formData:FormData = new FormData();
for (var i = 0; i < fileListLength; i++) {
formData.append("uploadFile[]", fileList[i]);
}
let headers = new Headers();
headers.append('Content-Type', 'multipart/form-data');
headers.append('Accept', 'application/json');
let options = new RequestOptions({ headers: headers });
this.http.post("http://localhost:8080/BCWEB/uploadProdImage", formData,
options)
.map(res => res.json())
.catch(error => Observable.throw(error))
.subscribe(
data => console.log('success'),
error => console.log(error)
)}
}
For the backend java ee restful web service:
String[] contentDispositionHeader = headers.getFirst("Content-
Disposition").split(";");
for (String name : contentDispositionHeader) {
if ((name.trim().startsWith("filename"))) {
String[] tmp = name.split("=");
String fileName = tmp[1].trim().replaceAll("\"","");
return fileName;
}
}
return "randomName";
}
// save uploaded file to a defined location on the server
private void saveFile(InputStream uploadedInputStream,
String serverLocation) {
try {
OutputStream outpuStream = new FileOutputStream(new
File(serverLocation));
int read = 0;
byte[] bytes = new byte[1024];
outpuStream = new FileOutputStream(new File(serverLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
outpuStream.write(bytes, 0, read);
}
outpuStream.flush();
outpuStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
**的问题是,我得到这个:
java.io.IOException:RESTEASY007550:无法获得多部分的边界“**
干草,很抱歉,但它仍然给我由此造成的同样的问题 –
:java.io.IOException异常:RESTEASY007550:无法获得多边界 –