<mx:Image source="@Embed('C:/test.PNG')" />
但是很快发现,Embed标签是在编译期就把图片加载进swf文件中的,无法实现动态更换图片的功能。
解决的办法是:
新建一个Servlet来实现图片读取。
ImageShowServlet.java
public class ImageShowServlet extends HttpServlet {
private static final long serialVersionUID = -3481832826439542244L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
processRequest(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
processRequest(req, resp);
}
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String imgName = request.getParameter("imagename");
ResourceBundle resourceBundel = ResourceBundle.getBundle("application");
String imgPath = resourceBundel.getString("img_root");
File imgFile = new File(imgPath + imgName);
if(imgFile.exists()) {
response.reset();
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "filename=" + imgName);
InputStream is = new FileInputStream(imgFile);
OutputStream outputStream = response.getOutputStream();
int data = -1;
while((data = is.read()) != -1) {
outputStream.write(data);
}
outputStream.flush();
outputStream.close();
}
}
}
Flex通过访问这个Servlet来实现图片加载:
var imgName : String = "test.PNG";
var imgUrl : String = "./imageshow?imagename=" + imgName;
imgroot.source = imgUrl; //imgroot.load(imgUrl);也可以