栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Java中的Rest API上传文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Java中的Rest API上传文件

我在下面的类中更新了方法的签名,并且它的工作正常。而不是

@FormParam
使用
@FormDataParam("path")
String路径,它解决了我的问题。下面是更新的代码:

import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.ws.rs.Consumes;import javax.ws.rs.FormParam;import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.core.MediaType;import javax.ws.rs.core.Response;import com.sun.jersey.core.header.FormDataContentDisposition;import com.sun.jersey.multipart.FormDataParam;@Path("/file")public class UploadFileService {@POST@Path("/upload")@Consumes(MediaType.MULTIPART_FORM_DATA)public Response uploadFile(        @FormDataParam("file") InputStream uploadedInputStream,        @FormDataParam("file") FormDataContentDisposition fileDetail,        @FormDataParam("path") String path) {    // Path format //10.217.14.97/Installables/uploaded/    System.out.println("path::"+path);    String uploadedFileLocation = path + fileDetail.getFileName();    // save it    writeToFile(uploadedInputStream, uploadedFileLocation);    String output = "File uploaded to : " + uploadedFileLocation;    return Response.status(200).entity(output).build();}// save uploaded file to new locationprivate 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();    }   }   }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/507457.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号