上传文件休息webservice

时间:2014-05-29 10:06:12

标签: web-services rest file-upload upload jersey

我正在使用此代码上传文件

public class upload {
private static final String SERVER_UPLOAD_LOCATION_FOLDER = "C://Users/Marya/Desktop/uploaded/";
@Path(value ="upload/{path}")
public Response uploadFile(
    @PathParam("path") String path, InputStream uploadedInputStream)
{
    String fileName = SERVER_UPLOAD_LOCATION_FOLDER + "randomName.png";
    saveFile(uploadedInputStream, fileName);
    String output = "File saved to server location : " + fileName;
    return Response.status(200).entity(output).build();
}
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();
    }
}

}

如果我使用此网址,则此代码无效:http:// * :8080 / ConnectDataBase / rest / files / upload / C:/Users/Marya/Desktop/Capture.jpg;

对此问题有任何建议吗?请帮忙!

0 个答案:

没有答案