这里的上传是针对jfinal的
代码如下:
public void uploadFile(){
UploadFile uploadFile=this.getFile();
String fileName=uploadFile.getOriginalFileName();
File file=uploadFile.getFile();
FileService fs=new FileService();
File t=new File("D:\\桌面\\"+UUID.randomUUID().toString());
try {
t.createNewFile();
fs.fileChannelCopy(file, t);
setAttr("tip",tip);
} catch (IOException e) {
e.printStackTrace();
}
file.delete();
redirect("/jf/platform/projectdeclareperson");
}
对应的Fileservice这个类与文件复制方法fileChannelCopy:
public class FileService {
public void fileChannelCopy(File s, File t) {
FileInputStream fi = null;
FileOutputStream fo = null;
FileChannel in = null;
FileChannel out = null;
try {
fi = new FileInputStream(s);
fo = new FileOutputStream(t);
in = fi.getChannel();// 得到对应的文件通道
out = fo.getChannel();// 得到对应的文件通道
in.transferTo(0, in.size(), out);// 连接两个通道,并且从in通道读取,然后写入out通道
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fi.close();
in.close();
fo.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
<form id="splitPage" class="form-horizontal" action="${cxt!}/jf/platform/projectdeclareperson/uploadFile" enctype="multipart/form-data" method="post">
<input type="file" name="file"/>
<input type="submit"/>
</form>
这个是针对一般下载:
public void download1() throws Exception{
String cxt =PathKit.getWebRootPath();
ProjectDeclarePerson person = ProjectDeclarePerson.dao.findById(getPara());
String realname =person.getStr("realname");
File file =new File(cxt+"/WEB-INF/files/upload/"+realname);
this.getResponse().setContentType("application/x-msdownload");
this.getResponse().setHeader("Content-Disposition", "attachment;filename="+cxt+"/WEB-INF/files/upload/"+realname);
InputStream inputStream = new FileInputStream(file);
ServletOutputStream outputStream = this.getResponse().getOutputStream();
byte b[]=new byte[1024];
int n;
while((n=inputStream.read(b))!=-1){
outputStream.write(b,0,n);
}
outputStream.close();
inputStream.close();
}
针对jfinal的下载:
public void download() throws ServletException, IOException {
String ids =getPara();
String img =PathKit.getWebRootPath();
File file =new File(img+"/index.txt");
renderFile(file);
}
下载前台代码:
<a href="/jf/platform/projectdeclareperson/download/${escapeXml(projectdeclareperson.ids!)}">下载哦</a>