java 图片显示用什么(在java 中在代码中要引用的图片该放在哪个文件)
大家好,今天小编来为大家解答以下的问题,关于java 图片显示用什么,在java 中在代码中要引用的图片该放在哪个文件这个很多人还不知道,现在让我们一起来看看吧!
JAVA 读取 数据库中的图片显示到页面
其实在大多数的实际项目中数据库中存放的是图片的url地址直接将图片放置到你的项目中的一个专门的文件夹先在页面显示的时候直接按照 url来显示图片这样既方便由能够缓解服务器负担增加性能你可以尝试一下
<img src="你从数据库中获得的图片的url">
想必读取数据库你应该会把
在java 中在代码中要引用的图片该放在哪个文件
放到任意文件夹都可以,看你的管理方式。
1、放到和程序目录下的某个文件夹中。
//假设图片放到程序运行目录的img目录下
BufferedImageimg=ImageIO.read(newFile("img/my.png"));
2、放到源文件中,和读取类在同一目录,使用时图片要按包名打包到jar中
//假设图片放到src下,和MyImages在一个目录
BufferedImageimg=ImageIO.read(MyImages.class.getResource("my.png"));
3、放到源文件中,但在独立文件夹中,使用时图片要按包名打包到jar中
//假设图片放到src下的img目录中
BufferedImageimg=ImageIO.read(MyImages.class.getResource("res/my.png"));
java实现图片上传至服务器并显示,如何做
给你段代码,是用来在ie上显示图片的(servlet):
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String id= request.getParameter("id");
File file= new File(getServletContext().getRealPath("/")+"out"+"/"+id+".gif");
response.setCharacterEncoding("gb2312");
response.setContentType("doc");
response.setHeader("Content-Disposition","attachment; filename="+ new String(file.getName().getBytes("gb2312"),"iso8859-1"));
System.out.println(new String(file.getName().getBytes("gb2312"),"gb2312"));
OutputStream output= null;
FileInputStream fis= null;
try
{
output= response.getOutputStream();
fis= new FileInputStream(file);
byte[] b= new byte[1024];
int i= 0;
while((i= fis.read(b))!=-1)
{
output.write(b, 0, i);
}
output.write(b, 0, b.length);
output.flush();
response.flushBuffer();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(fis!= null)
{
fis.close();
fis= null;
}
if(output!= null)
{
output.close();
output= null;
}
}
}
这个程序的功能是根据传入的文件名(id),来为浏览器返回图片流,显示在<img>标签里
标签的格式写成如下:
<img src="http://localhost:8080/app/preview?id=111"/><br/>
显示的是111.gif这个图片
你上面的问题:
1.我觉得你的第二个办法是对的,我们也是这样做的,需要的是把数据库的记录id号传进servlet,然后读取这条记录中的路径信息,生成流以后返回就是了
关于上传文件的问题,我记得java中应该专门有个负责文件上传的类,你调用就行了,上传后存储在指定的目录里,以实体文件的形式存放
你可以参考这个:
http://blog.csdn.net/arielxp/archive/2004/09/28/119592.aspx
回复:
1.是的,在response中写入流就行了
2.是发到servlet中的,我们一般都是写成servlet,短小精悍,使用起来方便,struts应该也可以,只是我没有试过,恩,你理解的很对
java中怎么将word文档怎么生成图片
public class CreateWordDemo
{
public void createDocContext(String file)
throws DocumentException,IOException{
//
设置纸张大小
Document document= new
Document(PageSize.A4);
//
建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
RtfWriter2.getInstance(document, new
FileOutputStream(file));
document.open();
//
设置中文字体
BaseFont bfChinese=
BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
//
标题字体风格
Font titleFont= new Font(bfChinese, 12,
Font.BOLD);
//
正文字体风格
Font contextFont= new Font(bfChinese, 10,
Font.NORMAL);
Paragraph title= new
Paragraph("标题");
//
设置标题格式对齐方式
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
document.add(title);
String contextString=
"iText是一个能够快速产生PDF文件的java类库。"
+"\n"//
换行
+
"iText的java类对于那些要产生包含文本,"
+"表格,图形的只读文档是很有用的。它的类库尤其与java
Servlet有很好的给合。"
+
"使用iText与PDF能够使你正确的控制Servlet的输出。";
Paragraph context= new
Paragraph(contextString);
//
正文格式左对齐
context.setAlignment(Element.ALIGN_LEFT);
context.setFont(contextFont);
//
离上一段落(标题)空的行数
context.setSpacingBefore(5);
//
设置第一行空的列数
context.setFirstLineIndent(20);
document.add(context);
//
利用类FontFactory结合Font和Color可以设置各种各样字体样式
Paragraph underline= new Paragraph("下划线的实现",
FontFactory.getFont(
FontFactory.HELVETICA_BOLDOBLIQUE, 18,
Font.UNDERLINE, new Color(0, 0,
255)));
document.add(underline);
//设置 Table
表格
Table aTable= new
Table(3);
int width[]={ 25, 25, 50
};
aTable.setWidths(width);//
设置每列所占比例
aTable.setWidth(90);//占页面宽度
90%
aTable.setAlignment(Element.ALIGN_CENTER);//
居中显示
aTable.setAlignment(Element.ALIGN_MIDDLE);//
纵向居中显示
aTable.setAutoFillEmptyCells(true);//
自动填满
aTable.setBorderWidth(1);//
边框宽度
aTable.setBorderColor(new Color(0, 125, 255));//
边框颜色
aTable.setPadding(2);//
衬距,看效果就知道什么意思了
aTable.setSpacing(3);//
即单元格之间的间距
aTable.setBorder(2);//
边框
//
设置表头
Cell haderCell= new
Cell("表格表头");
haderCell.setHeader(true);
haderCell.setColspan(3);
aTable.addCell(haderCell);
aTable.endHeaders();
Font fontChinese= new Font(bfChinese, 12, Font.NORMAL,
Color.GREEN);
Cell cell= new Cell(new Phrase("这是一个测试的 3*3 Table数据",
fontChinese));
cell.setVerticalAlignment(Element.ALIGN_TOP);
cell.setBorderColor(new Color(255, 0,
0));
cell.setRowspan(2);
aTable.addCell(cell);
aTable.addCell(new
Cell("#1"));
aTable.addCell(new
Cell("#2"));
aTable.addCell(new
Cell("#3"));
aTable.addCell(new
Cell("#4"));
Cell cell3= new Cell(new Phrase("一行三列数据",
fontChinese));
cell3.setColspan(3);
cell3.setVerticalAlignment(Element.ALIGN_CENTER);
aTable.addCell(cell3);
document.add(aTable);
document.add(new
Paragraph("\n"));
//
添加图片 Image.getInstance即可以放路径又可以放二进制字节流
Image img=
Image.getInstance("d:\\img01800.jpg");
img.setAbsolutePosition(0,
0);
img.setAlignment(Image.RIGHT);//
设置图片显示位置
img.scaleAbsolute(60, 60);//
直接设定显示尺寸
//
img.scalePercent(50);//表示显示的大小为原尺寸的50%
// img.scalePercent(25,
12);//图像高宽的显示比例
//
img.setRotation(30);//图像旋转一定角度
document.add(img);
document.close();
}
public static void main(String[] args)
{
CreateWordDemo word= new
CreateWordDemo();
String file=
"d:/demo1.doc";
try
{
word.createDocContext(file);
} catch(DocumentException e)
{
e.printStackTrace();
} catch(IOException e)
{
e.printStackTrace();
}
}
}
OK,本文到此结束,希望对大家有所帮助。