简体   繁体   English

无法使用Rest Web服务上传文件

[英]Cant upload a file using rest web service

I need to upload a file(image or any file) to server using java web service. 我需要使用Java Web服务将文件(图像或任何文件)上传到服务器。 The code which am used is mentioned. 提到了所使用的代码。 But unfortunately it doesn't work. 但不幸的是,它不起作用。

My Code: 我的代码:

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;


@Path("/webService")
public class ImageUpload {
    @POST
    @Path("/uploadImage")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public String uploadFile(@FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail)
            {
        saveToDisk(uploadedInputStream,fileDetail);
        return "File Uploaded Successfully";
            }

    private void saveToDisk(InputStream uploadedInputStream,
            FormDataContentDisposition fileDetail) {
        // TODO Auto-generated method stub
        String uploadFileLocation="d://upload/" + fileDetail.getFileName();
        try
        {
            OutputStream out=new FileOutputStream(new File(uploadFileLocation));
            int read=0;
            byte[] bytes=new byte[1024];
            out=new FileOutputStream(new File(uploadFileLocation));
            while((read=uploadedInputStream.read(bytes))!=-1)
            {
                out.write(bytes, 0, read);

            }
            out.flush();
            out.close();
        }
        catch(Exception io)
        {
            io.printStackTrace();
        }
    }
}

My Html file: 我的HTML文件:

<html>
<body>
<h1>Upload file to a RFestFul Web Service</h1>
<form action="http://localhost:8080/ImageUpload_demo/webService/uploadImage" method="post" enctype="multipart/form-data">
<label for="file"> Select a file to be uploaded</label>
<input type="file" name="file"> <br>
<input type="submit" value="Upload">
</form>

</body>
</html>

After running it throws, the requested resource is not available. 运行引发之后,请求的资源不可用。 And file is not uploaded at the specified location. 并且文件未上传到指定位置。 May someone helps to get it out. 可能有人帮助把它弄出来。 Am new to this part. 这部分是新手。

This is most likely due to a file-system rights issue. 这很可能是由于文件系统权限问题引起的。 Your service account running the application probably does not have write access to d:\\upload folder path. 您运行该应用程序的服务帐户可能没有对d:\\ upload文件夹路径的写权限。 Based on your path, I am assuming you're running your web service on a Windows box. 根据您的路径,我假设您正在Windows框上运行Web服务。 You will need to provide write privileges to the user account that starts the web server serving your service. 您需要为启动服务您的Web服务器的用户帐户提供写权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM