泽西上传文件

时间:2014-05-29 14:51:53

标签: java web-services rest jersey

我使用此代码使用jersey上传文件

@Path(value="/file")
public class upload {
@POST
@Path(value = "/upload/{path}")
@Consumes("image/png")
public Response uploadPng(@PathParam("path") String path, File file) throws IOException     {
    file = new File("C://Users/Marwa/Desktop/"+ path);
    String uploadedFileLocation = "C:/Users/Marwa/Desktop/workspace/" + file.getName();
    DataInputStream diStream =new DataInputStream(new FileInputStream(file));
    long len = (int) file.length();
    byte[] fileBytes = new byte[(int) len];
    int read = 0;
    int numRead = 0;
    while (read < fileBytes.length && (numRead =diStream.read(fileBytes, read,fileBytes.length - read)) >= 0) {
    read = read + numRead;
    }
    writeToFile(diStream, uploadedFileLocation);
    System.out.println("File uploaded to : " + uploadedFileLocation);
    return Response.status(200).entity(file).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:// $$$$:8080 / Ws / rest / files / upload / sms.png)我什么也没得到。对这个问题有任何建议吗?我不知道我的代码中是否有错误或我调用网络服务的方式是错误的!

0 个答案:

没有答案