jsp页面
form中的enctype属性应设置为:multipart/form-data
function fz(){
var aa=document.getElementById("file").value;
var arrS2=aa.split("\\");
var i=arrS2.length;
var filename= arrS2[i-1]
var arrb= aa.split("\.");
var j=arrb.length;
document.getElementById("file").value=aa;
document.getElementById("filename").value=filename;
document.getElementById("filepath").value="/uploadfiles/casus/"+filename;
document.getElementById("suffix").value=arrb[j-1];
}
action:
//附件相关
privateAttachmentMatters attachment;private String fileName; //名字
private String suffix; //后缀
privateString file;private String attachmentid;
/*** 上传*/
public voiduploadFile(){try{
String name=attachment.getFilename();
upload(this.file,name) ; //上传到服务器
//保存到附件表里
if(name!=null&&!name.equals("")){
attachment.setFilepath("uploadfiles/sss/"+name);
attachment.setUploadDate(new Date());
bean.saveObject(attachment);
}
}catch(Exception e){
e.printStackTrace();
}
}
public static voidupload(String file, String name){if(file !=null){
FileOutputStream outputStream;try{
String path=ServletActionContext.getRequest().getRealPath("/uploadfiles/matters");
String path1=path;
path1.replaceAll("\\u002E\\u002E", "2");
String fileDir= path+File.separator;
String filePath=fileDir+name;
File f=newFile(fileDir);
f.mkdirs();
outputStream= newFileOutputStream(filePath);
FileInputStream fileIn= newFileInputStream(file);byte[] buffer = new byte[128];intlen;while ((len = fileIn.read(buffer))>0){
outputStream.write(buffer,0, len);
}
fileIn.close();
outputStream.close();
}catch(Exception e) {
e.printStackTrace();
}
}else{
System.out.println("文件为空");
}
}
该博客内容涉及Java Web中jsp页面form的enctype属性设置,用于上传文件。通过JavaScript获取文件名和后缀,并在后台进行文件保存到服务器及附件表的存储操作。主要涉及文件流处理和文件路径构建。

被折叠的 条评论
为什么被折叠?



