//upFile.jsp
< import="java.util.List;"%>
上传文件function check(){
var file = document.getElementById("file1").value;
if(file == ""){
alert('请选择要上传的文件');
return false;
}else{
window.returnValue=file;
return true;
}
}
String files=request.getParameter("files");
%>
|
//commonfile.jsp
< import="java.util.*,org.apache.commons.fileupload.FileItem" %>
< import="java.text.SimpleDateFormat,java.util.Date" %>
< import="com.PoliceSystem.tools.FileOperate" %>
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
上传文件request.setCharacterEncoding("UTF-8");
String path1 = (String)request.getRealPath("/upload1");
String files=request.getParameter("files");
String[] fileList=files.split(";");
File file = new File(path1);
if(!file.exists()){
file.mkdirs();
}
factory.setRepository(file);
factory.setSizeThreshold(1024*1024);
upload.setFileItemFactory(factory);
try{
List list= upload.parseRequest(request);
for(FileItem item:list){
if(item.isFormField()){
//String value=item.getString("UTF-8");
//session.setAttribute("fileName", value);
}else{
String value=item.getName();
int start=value.lastIndexOf("\\");
String fileName=value.substring(start+1);
if(fileName.length() == 0 || fileName == ""){
out.println("");
}else{
if(item.getSize()>10000000){
out.println("");
}else{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date date =new Date();
String[] f = item.getName().split("\\\\");
//System.out.println(f[f.length-1]);
String oldFileName = f[f.length-1];
for(int i=0;i
if(fileList[i].indexOf(oldFileName)!=-1){
out.println("");
}
}
String fType = FileOperate.getFileType(item.getName());//文件类型
fileName = sdf.format(date)+"."+fType;//新文件名
//System.out.println(item.getName()+"---"+fileName);
item.write(new java.io.File(path1,fileName));
//--1--/
String mailFileNames = new String();
String old = (String)session.getAttribute("fuJianFileNames");
if(old!=null){
mailFileNames = old;
}
mailFileNames+=oldFileName+"|"+fileName+";";
//System.out.println("mailFileNames="+mailFileNames);
session.removeAttribute("fuJianFileNames");
session.setAttribute("fuJianFileNames", mailFileNames);
//--2--/
//String pathName = path1+";
//System.out.println("pathName="+pathName);
out.println("");
}
}
}
}
}catch (Exception e) {
e.printStackTrace();
out.println("");
}
%>
插入文件数据到数据库中
public boolean insert_annex(String[] str){
boolean b=true;
String sql ="";
con = db.getConn();
try{
sql="insert into annex (querykey,sfilename,committime,Filetype,filepath,pno,Annex) values (?,?,?,?,?,?,?)";
ps = con.prepareStatement(sql);
for(int i=0;i
ps.setString(i+1, str[i]);
}
File file = new File(str[6]);//附件
InputStream iso = new FileInputStream(file);
ps.setBinaryStream(7, iso, iso.available());
ps.execute();
iso.close();
System.out.println("删除临时文件:" + file.delete());// 删除tmp文件
}catch(Exception e){
b=false;
e.printStackTrace();
}finally{
this.close();
}
return b;
}
读出文件数据,并保存
public boolean addTempFile_annex(String querykey,HttpServletRequest request){
boolean b=true;
con = db.getConn();
InputStream in = null;
OutputStream out= null;
String path1 = (String)request.getRealPath("/upload1");//文件下载临时目录
try{
String sql="select filePath,annex from annex where querykey='"+querykey+"'";
//String sql_1="select annex from annex where querykey='"+querykey+"'";
ps = con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
File f = new File(path1+rs.getString("filepath"));
in=rs.getBinaryStream("annex");
out = new FileOutputStream(f);
int len = 10 * 100 * 100; //定义字符数组长度
byte[] P_Buf=new byte[len];
int j;
while((j=in.read(P_Buf))!=-1){
out.write(P_Buf, 0, j);
}
}
in.close();
out.flush(); //强制清出缓冲区
out.close();
}catch(Exception e){
b=false;
e.printStackTrace();
}finally{
this.close();
}
return b;
}
//给定传递过来的参数(文件名,文件存储在服务器的文件名,文件在服务器的路径,文件类型),下载文件Action
package com.PoliceSystem.action.mail;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class downLoadFile extends ActionSupport{
private static final long serialVersionUID = -2207648627734251737L;
public String execute() throws Exception{
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
String fileminitype = request.getParameter("fileType");
String filename1 = new String(request.getParameter("fileName1").getBytes("ISO8859-1"),"UTF-8");
String filename2 = new String(request.getParameter("fileName2").getBytes("ISO8859-1"),"UTF-8");
String filepath = request.getRealPath("/upload1");
File f = new File(filepath+");
Long filelength = f.length();
int cacheTime = 10;
response.setContentType(fileminitype);
response.setHeader("Location",filename1);
response.setHeader("Cache-Control", "max-age=" + cacheTime);
response.setContentType("application/octet-stream");
byte[] b = filename1.getBytes("GBK");
filename1 = new String(b,"8859_1");
response.setHeader("Content-Disposition", "attachment;filename=" + filename1);
response.setContentLength(filelength.intValue());
OutputStream outputStream = response.getOutputStream();
InputStream inputStream = new FileInputStream(f);
byte[] buffer = new byte[1024];
int i = -1;
while ((i = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, i);
}
outputStream.flush();
outputStream.close();
inputStream.close();
return null ;
}
}