主要的後臺java代碼html
/*** * 文件拷貝方法 * * @throws Exception */ private void copyFile() throws Exception { if (file == null) { return; } String[] c = new SimpleDateFormat("yyyy,MM,dd").format(new Date()) .split(","); StringBuffer sb = new StringBuffer(); for (String temp : c) { sb.append(temp + "/"); } PubUser user = (PubUser) ActionContext.getContext().getSession() .get("SystemUserToken"); // 獲取登錄用戶 File dir = new File(this.imgFileStore.getRootFile().getPath() + "/photo/" + addressList.getUserid()); // 上傳的路徑在 project.properties 裏面改 System.out.println("dir" + dir); if (!dir.exists()) { dir.mkdirs(); } String newfilename = new Date().getTime() + fileFileName.substring(fileFileName.lastIndexOf("."));// 獲取時間戳 // 生成隨機文件名 File new_File = new File(dir, newfilename); FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(new_File); byte[] b = new byte[512]; // 修改文件名 int a = 0; while ((a = fis.read(b)) != -1) { fos.write(b, 0, a); } fis.close(); fos.close(); addressList.setPhoto(newfilename); //newfilename生成的文件名字,下面是拼路徑 addressList.setPhotourl("photo/" + addressList.getUserid() + "/" + newfilename); }
前臺jsp代碼java
function file_name() { $('#filename').val($('#file_photo').val()); } function choice() { $('#file_photo').click(); $('#zy') .html( "<input type='file' name='file' id='file_photo' onblur='yhcf2(this)' class='input_bg' style='width:180px;' onchange='file_name()'/><input name='filename' type='hidden' id='filename' onblur='yhcf2(this)'/>"); } //上面是js 下面是頁面代碼 <input type="file" name="file" id="file_photo" class="input_bg" style="width:180px;" onchange="file_name()" onblur="yhcf2(this)" /> <input name="filename" type="hidden" id="filename" onblur="yhcf2(this)" />