int id = (Integer) request.getSession().getAttribute("id");
公司主營(yíng)業(yè)務(wù):網(wǎng)站設(shè)計(jì)、做網(wǎng)站、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。創(chuàng)新互聯(lián)推出山南免費(fèi)做網(wǎng)站回饋大家。
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(factory);
sfu.setFileSizeMax(1024 * 1024);//1M
ListFileItem items = sfu.parseRequest(request);
for (int i = 0; i items.size(); i++) {
FileItem item = items.get(i);
if (!item.isFormField()) {
ServletContext sctx = getServletContext();
String picurl = sctx.getRealPath("upload");
String fileName = item.getName();
fileName = fileName.substring(fileName
.lastIndexOf("\\") + 1);
PicDao picDao = new PicDaoImpl();
System.err.println(id);
picDao.savePic(id, fileName);
File file = new File(picurl + "/pic_"
+ StringUtils.leftPad(id + "", 5, '0') + "/"
+ fileName);
item.write(file);
}
}
response.sendRedirect("userDetail.do?id=" + id);
public class FileUpLoad extends ActionSupport{
//"多文件上傳就用list就可以了private ListFile file;"
private File file;
//上傳文本的name
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
private String fileContentType;
//上傳的文件類型。
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
//獲取上傳文件的名稱
private String fileFileName;
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String upload() throws Exception
{
//獲取文件上傳路徑
String root=ServletActionContext.getRequest().getRealPath("/upload");
InputStream is=new FileInputStream(file);
String.substring(fileFileName.indexOf("."));//截取上傳文件的后綴。便于新定義名稱。.jpg
System.out.println(name);
File descFile=new File(root,新定義的文件名稱+fileFileName.indexOf("."));
OutputStream os=new FileOutputStream(descFile);
byte[] buffer=new byte[1024];
int length=0;
while(-1!=(length=(is.read(buffer))))
{
os.write(buffer, 0, length);
}
is.close();
os.close();
return SUCCESS;
}
}
上傳附件,實(shí)際上就是將文件存儲(chǔ)到遠(yuǎn)程服務(wù)器,進(jìn)行臨時(shí)存儲(chǔ)。舉例:
**
* 上傳文件
*
* @param fileName
* @param plainFilePath 文件路徑路徑
* @param filepath
* @return
* @throws Exception
*/
public static String fileUploadByFtp(String plainFilePath, String fileName, String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try {
fis = new FileInputStream(plainFilePath);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
Log.info("加密上傳文件開(kāi)始");
Log.info("連接遠(yuǎn)程上傳服務(wù)器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl="true";
ftpClient.changeWorkingDirectory("/"+filepath+"");
}
}
Log.info("檢查文件路徑是否存在:/"+filepath);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查詢文件路徑不存在:"+"/"+filepath);
return bl;
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 設(shè)置文件類型(二進(jìn)制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, fis);
Log.info("上傳文件成功:"+fileName+"。文件保存路徑:"+"/"+filepath+"/");
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}
}
備注:只需要修改上傳的服務(wù)器地址、用戶名、密碼即可進(jìn)行服務(wù)器訪問(wèn)上傳。根據(jù)實(shí)際需要修改即可。