0
我正在開發一個用於文件上傳的Web服務。 由於此服務僅在其參數中使用字符串,而該參數是文件的絕對路徑。 然後使用java Stream類,我可以寫入我的目標目錄。 但是,這個Web服務將如何被客戶訪問。 客戶將如何使用它。 這裏有什麼建議,我應該怎麼做。通用文件上傳Web服務
我如果這被認爲是Web服務在這裏
@Path("/file")
public class FileUploadService
{
static String fileDestination = "/home/user/mywebservice/uploads/";
@POST
@Path("/upload")
public void fileUpload(String fileSource)throws Exception
{
java.nio.file.Path p = Paths.get(fileSource);
String s1=fileDestination+p.getFileName();
FileInputStream fin = new FileInputStream(new File(fileSource));
FileOutputStream fout = new FileOutputStream(new File(s1));
int read = 0;
byte[] bytes = new byte[1024];
fout = new FileOutputStream(new File(s1));
while ((read = fin.read(bytes)) != -1)
{
fout.write(bytes, 0, read);
}
fout.flush();
fout.close();
System.out.println("File uploaded to "+ s1);
}
}
這取決於你如何構建你的應用程序。如果這是爲客戶端上傳文件,那麼,如下所示,UI將是必要的。否則,如果它被設計成只有其他應用程序/系統使用它來上傳文件,則不需要UI。 – Ironluca
我想我可以把@FormParam(「文件名」)註釋在方法的參數之前。這可以通過curl來測試。 –