上传:
public ActionForward upload(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String path = getServlet().getServletContext().getRealPath("/");
if (form == null) {
return mapping.findForward("success");
}
FileUploadForm fuf = (FileUploadForm) form;
FormFile file = fuf.getFile();
try {
if (file == null) {
return mapping.findForward("success");
}
} catch (Exception e) {
e.printStackTrace();
}
String fname = file.getFileName();
int t = 0;
for (int i = 0; i < fname.length(); i++) {
if (fname.charAt(i) == ‘.‘) {
t = i;
}
}
/**
* 文件名
*/
String filename = null;
if (t > 0) {
filename = fname.substring(0, t);
}
ServletContext servletContext = servlet.getServletContext();
ApplicationContext ctx = WebApplicationContextUtils
.getRequiredWebApplicationContext(servletContext);
LawCaseDAO lawCaseDAO = (LawCaseDAO) ctx.getBean("lawCaseDAO");
Tajjbxx lawcase = lawCaseDAO.view(fuf.getFid());
String urlFileName = lawcase.getBaAjbh() + "_" + fuf.getLx();
urlFileName += fname.substring(t);
/**
* 保存文件路径
*/
String filepath = "/pages/lawcase/wenshu/" + urlFileName;
/**
* 先生成文件保存的路径
*/
if (!(new File(path + "/pages/lawcase/wenshu/")).exists()) {
(new File(path + "/pages/lawcase/wenshu/")).mkdirs();
}
/**
* 将上传的文书信息保存到数据库中
*/
Tajws wenshu = new Tajws();
/**
* 设置文书所对应的案件id
*/
wenshu.setAjseqid(new Long(fuf.getFid()));
/**
* 设置文书的名称
*/
wenshu.setWsmc(filename);
/**
* 设置文书的保存路径
*/
wenshu.setWsbclj(filepath);
/**
* 设置文书的类型
*/
wenshu.setWslx(fuf.getLx());
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
wenshu.setScrq((java.sql.Date.valueOf(format.format(date))));
/**
* 检测文书是新增还是覆盖
*/
Tajws ws = lawCaseDAO.checkWenshuExist(wenshu);
if (ws == null) {// 新增
lawCaseDAO.addWenshu(wenshu);
} else {// 修改
/**
* 先删除后生成
*/
File f = new File(path + ws.getWsbclj());
f.delete();
ws.setWsmc(filename);
ws.setWsbclj(filepath);
lawCaseDAO.updateWenshu(ws);
}
/**
* 创建文件
*/
OutputStream os = new FileOutputStream(path + "/pages/lawcase/wenshu/"
+ urlFileName);
int readBytes = 0;
byte buffer[] = new byte[8192];
InputStream stream = file.getInputStream();
while ((readBytes = stream.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, readBytes);
}
os.close();
stream.close();
file.destroy();
request.setAttribute("message", "上传成功!");
return mapping.findForward("success");
}
下载:
public ActionForward downloadFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String path = getServlet().getServletContext().getRealPath("/");
String sourceFilePathName = request.getParameter("fname");
File file = new File(path + sourceFilePathName);
long length = file.length(); // 文件长度
InputStream in = new FileInputStream(file);
if (in != null) {
String fs = file.getName();
response.reset();
response.setContentType("application/x-msdownload"); //
String s = "attachment; filename=" + fs; //
response.setHeader("Content-Disposition", s); // 以上输出文件元信息
response.setContentLength((int) length); // 设置输入文件长度
byte[] b = new byte[2048];
int len = 0;
while ((len = in.read(b)) > 0) {
response.getOutputStream().write(b, 0, len); // 向浏览器输出
}
in.close(); // 关闭文件输入流
}
return null;
}