我在Heroku中部署了一个服务,生成pdf文件输出.当我点击浏览器中的URL时,我可以下载pdf文件(我没有提示保存(根据我的要求),它会自动保存到代码中的已定义路径).所以服务已经开始并且可用.但是当我使用HttpURLConnection访问它时,我收到404错误..任何人都可以帮我解决这个问题吗?
以下是我访问的链接:http: //quiet-savannah-7144.herokuapp.com/services/time/temp
以下是部署在Heroku服务器中的服务代码:
@jawax.ws.rs.core.Context
ServletContext context;
@GET
@path("/temp")
@Produces("application/pdf")
public Response getPdf() throws IOException{
InputStream is = context.getResourceAsStream("/static/temp.pdf");
ResponseBuilder res = Response.ok(is);
res.header("Content-Disposition","attachment; filename=temp.pdf");
return res.build();
}
Run Code Online (Sandbox Code Playgroud)
注意:我的文件位于webapp/static/temp.pdf
客户端代码如下:
try {
URL url = new URL("http://quiet-savannah-7144.herokuapp.com/sevices/time/temp");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json");
int code = conn.getResponseCode();
System.out.println("code>>"+code+"<<");
if (conn.getResponseCode() == 200) {
System.out.println("*************************done****************************");
InputStream inputStream = conn.getInputStream();
OutputStream output = new FileOutputStream("D:/copyOfTest.pdf");
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != …Run Code Online (Sandbox Code Playgroud) 早上好.今天早上,当我通过泽西实体提供商MessageBodyReader和MessageBodyWriters时,我遇到了以下问题.
我想编写一个资源方法和客户端,它返回一个自定义对象列表和媒体类型application/xml.所以我想使用JAXB(我是JAXB的新手).我能够通过编写自己的扩展MessageBodyReader和来实现这一目标MessageBodyWriter.但我害怕我跟随的方式.看看我实施的方式:
资源方法:
@Path("productlist/xml")
@GET
public RetObjects getProductsXml(){
List<Product> pList = new ArrayList<Product>();
pList.add(new Product("1","Dell latitude E6000",2900,500));
pList.add(new Product("2","Xperia Z2",549,400));
RetObjects obj = new RetObjects();
obj.setObject(pList);
return obj;
}
Run Code Online (Sandbox Code Playgroud)
我的自定义对象:
@Entity
@Table (name="PRODUCT")
@XmlRootElement(name="product")
public class Product {
@Id
@Column(name = "CODE")
private String code;
...
// rest of the fields, constructors, getters and setters
}
Run Code Online (Sandbox Code Playgroud)
包装我的自定义对象列表的对象:
@XmlRootElement(name = "products")
@XmlAccessorType (XmlAccessType.FIELD)
public class RetObjects {
@XmlElement(name = "product")
private …Run Code Online (Sandbox Code Playgroud) 我有两个字符串
aisozp;ak apso;; ;pasix acd;XYZ;ao aoz;; ;aixi o oiz
aisozp;ak apso;;XYZ;pasix acd;098;ao aoz;; ;XYZ; as oiz
Run Code Online (Sandbox Code Playgroud)
这里我需要用ABC替换字符串XYZ,只有当它存在于第5和第6分号(;)之间时.我尝试了以下的事情来实现这一目标
data.replaceAll("(((.*?);){5})XYZ", "$1ABC")
Run Code Online (Sandbox Code Playgroud)
第一个字符串工作正常.但对于第二个字符串,它将替换字符串XYZ(最后一次出现),后面没有第5个分号.这里如何将搜索限制为5.以上正则表达式试图匹配分号5及以上的XYZ我猜.注意:每个分号之间的数据长度是可变的.有人可以帮我吗?
提前致谢.