📄 albumpane.java
字号:
package com.cownew.phoneshow.ent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import nextapp.echo2.app.Button;
import nextapp.echo2.app.Grid;
import nextapp.echo2.app.ResourceImageReference;
import nextapp.echo2.app.Row;
import nextapp.echo2.app.WindowPane;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.filetransfer.Download;
import nextapp.echo2.app.filetransfer.UploadEvent;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import com.cownew.phoneshow.MainApp;
import com.cownew.phoneshow.framework.common.Resources;
import com.cownew.phoneshow.framework.common.ZipDownloadProvider;
import com.cownew.phoneshow.framework.ctrl.OpenFileDialog;
import com.cownew.phoneshow.framework.ctrl.UploadListenerAdapter;
import echopointng.ImageIcon;
import echopointng.PushButton;
public class AlbumPane extends WindowPane
{
// 相册的包路径
private String albumPath = "/album/" + MainApp.getActiveUserId() + "/";
// 相册的路径
// 为了避免多用户混淆,这里用用户id做包名
private String uploadImgPath = "./writable/" + albumPath;
private ImageIcon imgIcon;
private File[] files;
// 当前显示图片的索引号
private int curIndex;
public AlbumPane()
{
super();
setTitle("在线相册");
// 创建文件列表
createFiles();
// 创建布置界面的网格,只有一列
Grid grid = new Grid(1);
add(grid);
// 由于有两个组件(前进后退),所以首先将两个组件放到一个Row中,保证在一行中显示
Row rowNavigator = new Row();
Button btnPrev = new Button(Resources.ICON_24_LEFT_ARROW);
btnPrev.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
prev();
}
});
Button btnNext = new Button(Resources.ICON_24_RIGHT_ARROW);
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
next();
}
});
rowNavigator.add(btnPrev);
rowNavigator.add(btnNext);
grid.add(rowNavigator);
Row rowUpload = new Row();
grid.add(rowUpload);
PushButton btnUpload = new PushButton("上传");
rowUpload.add(btnUpload);
btnUpload.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
upload();
}
});
PushButton btnDownload = new PushButton("打包下载");
rowUpload.add(btnDownload);
btnDownload.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
download();
}
});
imgIcon = new ImageIcon();
// 将图片加到表格中
grid.add(imgIcon);
next();
}
private void createFiles()
{
File rootFile = new File(uploadImgPath);
// 如果目录不存在,则创建一个
// 因为用户第一次进入的时候是没有这个目录的
if (!rootFile.exists())
{
rootFile.mkdir();
}
files = rootFile.listFiles();
curIndex = -1;
}
private void download()
{
// 文件打包下载
Download download = new Download();
download.setProvider(new ZipDownloadProvider(files, "album.zip"));
MainApp.getActive().enqueueCommand(download);
}
private void upload()
{
OpenFileDialog openDlg = new OpenFileDialog(
new UploadListenerAdapter() {
public void fileUpload(UploadEvent e)
{
if (e.getContentType().startsWith("image/"))
{
saveUploadedImage(e);
// 文件上传以后刷新列表
createFiles();
} else
{
// 限制只能上传图片类型的文件
MainApp.showInfo("只能上传图片类型文件!");
}
}
});
MainApp.addComponent(openDlg);
}
// 显示下一张图片
private void next()
{
if (curIndex < files.length - 1)
{
curIndex++;
}
if (curIndex >= 0)
{
File file = files[curIndex];
imgIcon.setIcon(new ResourceImageReference(albumPath
+ file.getName()));
}
}
// 显示上一张图片
private void prev()
{
if (curIndex > 0)
{
curIndex--;
}
if (curIndex >= 0)
{
File file = files[curIndex];
imgIcon.setIcon(new ResourceImageReference(albumPath
+ file.getName()));
}
}
// 将上传的文件保存到本地
private void saveUploadedImage(UploadEvent e)
{
// 调用commons-io包中的FilenameUtils工具类得到文件的扩展名
String ext = FilenameUtils.getExtension(e.getFileName());
FileOutputStream fos = null;
InputStream inStream = e.getInputStream();
try
{
// 创建以ext为扩展名的新文件名
File file = createImageFile(ext);
fos = new FileOutputStream(file);
byte[] buff = new byte[1024];
int iCount;
while ((iCount = inStream.read(buff)) > 0)
{
fos.write(buff, 0, iCount);
}
} catch (FileNotFoundException fnfe)
{
MainApp.showInfo(fnfe.getMessage());
} catch (IOException ioe)
{
MainApp.showInfo(ioe.getMessage());
} finally
{
IOUtils.closeQuietly(fos);
IOUtils.closeQuietly(inStream);
}
}
/**
* 得到一个以ext为后缀的文件对象
*
* @param ext
* @return
*/
private File createImageFile(String ext)
{
String uuid = UUID.randomUUID().toString();
File file = new File(uploadImgPath + uuid + "." + ext);
return file;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -