真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java上傳附件功能代碼 java附件上傳功能實(shí)現(xiàn)

java 文件上傳的代碼,盡量詳細(xì)一點(diǎn)。。。

// 這是我寫的一個(gè)方法,里面只需要傳兩個(gè)參數(shù)就OK了,在任何地方調(diào)用此方法都可以文件上傳

成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計(jì),海淀網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:海淀等地區(qū)。海淀做網(wǎng)站價(jià)格咨詢:13518219792

/**

* 上傳文件

* @param file待上傳的文件

* @param storePath待存儲(chǔ)的路徑(該路徑還包括文件名)

*/

public void uploadFormFile(FormFile file,String storePath)throws Exception{

// 開始上傳

InputStream is =null;

OutputStream os =null;

try {

is = file.getInputStream();

os = new FileOutputStream(storePath);

int bytes = 0;

byte[] buffer = new byte[8192];

while ((bytes = is.read(buffer, 0, 8192)) != -1) {

os.write(buffer, 0, bytes);

}

os.close();

is.close();

} catch (Exception e) {

throw e;

}

finally{

if(os!=null){

try{

os.close();

os=null;

}catch(Exception e1){

;

}

}

if(is!=null){

try{

is.close();

is=null;

}catch(Exception e1){

;

}

}

}

}

怎么用java發(fā)送帶附件的郵件代碼詳解

package?email;??

import?java.io.BufferedReader;???

import?java.io.File;??

import?java.io.FileInputStream;??

import?java.io.FileNotFoundException;??

import?java.io.IOException;??

import?java.io.InputStream;??

import?java.io.InputStreamReader;??

import?java.io.PrintWriter;??

import?java.io.UnsupportedEncodingException;??

import?java.net.Socket;??

import?java.nio.charset.Charset;??

import?java.text.SimpleDateFormat;??

import?java.util.ArrayList;??

import?java.util.Date;??

import?java.util.HashMap;??

import?java.util.List;??

import?java.util.Map;??

import?sun.misc.BASE64Encoder;??

public?class?Mail?{??

private?static?final?String?LINE_END?=?"\r\n";??

private?boolean?isDebug?=?true;??

private?boolean?isAllowReadSocketInfo?=?true;??

private?String?host;??

private?String?from;??

private?ListString?to;??

private?ListString?cc;??

private?ListString?bcc;??

private?String?subject;??

private?String?user;??

private?String?password;??

private?String?contentType;??

private?String?boundary;??

private?String?boundaryNextPart;??

private?String?contentTransferEncoding;??

private?String?charset;??

private?String?contentDisposition;??

private?String?content;??

private?String?simpleDatePattern;??

private?String?defaultAttachmentContentType;??

private?ListMailPart?partSet;??

private?static?MapString,?String?contentTypeMap;??

static?{??

//?MIME?Media?Types??

contentTypeMap?=?new?HashMapString,?String();??

contentTypeMap.put("xls",?"application/vnd.ms-excel");??

contentTypeMap.put("xlsx",?"application/vnd.ms-excel");??

contentTypeMap.put("xlsm",?"application/vnd.ms-excel");??

contentTypeMap.put("xlsb",?"application/vnd.ms-excel");??

contentTypeMap.put("doc",?"application/msword");??

contentTypeMap.put("dot",?"application/msword");??

contentTypeMap.put("docx",?"application/msword");??

contentTypeMap.put("docm",?"application/msword");??

contentTypeMap.put("dotm",?"application/msword");??

}??

private?class?MailPart?extends?Mail?{??

public?MailPart()?{??

}??

}??

public?Mail()?{??

defaultAttachmentContentType?=?"application/octet-stream";??

simpleDatePattern?=?"yyyy-MM-dd?HH:mm:ss";??

boundary?=?"--=_NextPart_zlz_3907_"?+?System.currentTimeMillis();??

boundaryNextPart?=?"--"?+?boundary;??

contentTransferEncoding?=?"base64";??

contentType?=?"multipart/alternative";??

charset?=?Charset.defaultCharset().name();??

partSet?=?new?ArrayListMailPart();??

to?=?new?ArrayListString();??

cc?=?new?ArrayListString();??

bcc?=?new?ArrayListString();??

}??

private?String?getPartContentType(String?fileName)?{??

String?ret?=?null;??

if?(null?!=?fileName)?{??

int?flag?=?fileName.lastIndexOf(".");??

if?(0?=?flag??flag??fileName.length()?-?1)?{??

fileName?=?fileName.substring(flag?+?1);??

}??

ret?=?contentTypeMap.get(fileName);??

}??

if?(null?==?ret)?{??

ret?=?defaultAttachmentContentType;??

}??

return?ret;??

}??

private?String?toBase64(String?str,?String?charset)?{??

if?(null?!=?str)?{??

try?{??

return?toBase64(str.getBytes(charset));??

}?catch?(UnsupportedEncodingException?e)?{??

e.printStackTrace();??

}??

}??

return?"";??

}??

private?String?toBase64(byte[]?bs)?{??

return?new?BASE64Encoder().encode(bs);??

}??

private?String?toBase64(String?str)?{??

return?toBase64(str,?Charset.defaultCharset().name());??

}??

private?String?getAllParts()?{??

int?partCount?=?partSet.size();??

StringBuilder?sbd?=?new?StringBuilder(LINE_END);??

for?(int?i?=?partCount?-?1;?i?=?0;?i--)?{??

Mail?attachment?=?partSet.get(i);??

String?attachmentContent?=?attachment.getContent();??

if?(null?!=?attachmentContent??0??attachmentContent.length())?{??

sbd.append(getBoundaryNextPart()).append(LINE_END);??

sbd.append("Content-Type:?");??

sbd.append(attachment.getContentType());??

sbd.append(LINE_END);??

sbd.append("Content-Transfer-Encoding:?");??

sbd.append(attachment.getContentTransferEncoding());??

sbd.append(LINE_END);??

if?(i?!=?partCount?-?1)?{??

sbd.append("Content-Disposition:?");??

sbd.append(attachment.getContentDisposition());??

sbd.append(LINE_END);??

}??

sbd.append(LINE_END);??

sbd.append(attachment.getContent());??

sbd.append(LINE_END);??

}??

}??

sbd.append(LINE_END);??

sbd.append(LINE_END);??

partSet.clear();??

return?sbd.toString();??

}??

private?void?addContent()?{??

if?(null?!=?content)?{??

MailPart?part?=?new?MailPart();??

part.setContent(toBase64(content));??

part.setContentType("text/plain;charset=\""?+?charset?+?"\"");??

partSet.add(part);??

}??

}??

private?String?listToMailString(ListString?mailAddressList)?{??

StringBuilder?sbd?=?new?StringBuilder();??

if?(null?!=?mailAddressList)?{??

int?listSize?=?mailAddressList.size();??

for?(int?i?=?0;?i??listSize;?i++)?{??

if?(0?!=?i)?{??

sbd.append(";");??

}??

sbd.append("").append(mailAddressList.get(i)).append("");??

}??

}??

return?sbd.toString();??

}??

private?ListString?getrecipient()?{??

ListString?list?=?new?ArrayListString();??

list.addAll(to);??

list.addAll(cc);??

list.addAll(bcc);??

return?list;??

}??

public?void?addAttachment(String?filePath)?{??

addAttachment(filePath,?null);??

}??

public?void?addTo(String?mailAddress)?{??

this.to.add(mailAddress);??

}??

public?void?addCc(String?mailAddress)?{??

this.cc.add(mailAddress);??

}??

public?void?addBcc(String?mailAddress)?{??

this.bcc.add(mailAddress);??

}??

public?void?addAttachment(String?filePath,?String?charset)?{??

if?(null?!=?filePath??filePath.length()??0)?{??

File?file?=?new?File(filePath);??

try?{??

addAttachment(file.getName(),?new?FileInputStream(file),??

charset);??

}?catch?(FileNotFoundException?e)?{??

System.out.println("錯(cuò)誤:"?+?e.getMessage());??

System.exit(1);??

}??

}??

}

上傳附件 java 代碼

SmartUpload上傳圖片

記得下載jar包啊

別忘了把名改成smartUpload.jar

Utility類:

package com.tidyinfo.utils;

import java.util.Calendar;

public class Utility {

//生成形如:\2006\08\18的路徑

private String path;

//生成形如: 20060818125012的唯一id號(hào)

private String id;

//生成形如:/2006/08/18的路徑

private String url;

private String hour;

private String year;

private String month;

private String day;

private String minate;

private String second;

/** Creates a new instance of Utility */

public Utility() {

this.year =new Integer(Calendar.getInstance().get(Calendar.YEAR)).toString();

this.month = parseToString(Calendar.getInstance().get(Calendar.MONTH)+1);

this.day = parseToString(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));

this.hour = parseToString(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));

this.minate = parseToString(Calendar.getInstance().get(Calendar.MINUTE));

this.second = parseToString(Calendar.getInstance().get(Calendar.SECOND));

this.setId();

this.setPath();

this.setUrl();

}

protected String parseToString(int i){

return i10?("0"+i):new Integer(i).toString();

}

public String getId(){

return this.id;

}

public String getPath(){

return this.path;

}

public String getUrl(){

return this.url;

}

protected void setId(){

this.id = this.year+this.month+this.day+this.hour+this.minate+this.second;

}

protected void setPath(){

this.path = "\\"+this.year + "\\" + this.month + "\\" +

this.day ;

}

protected void setUrl(){

this.url = this.path.replace('\\','/');

}

}

Action:(根據(jù)不同情況要更改相關(guān)代碼)

User userForm = new User();

String result = new String();

SmartUpload su = new SmartUpload();

//文件名

String filename = new String();

//文件擴(kuò)展名

String fileext = new String();

//文件上傳后存儲(chǔ)的路徑

String path = request.getRealPath(request.getContextPath());

path = path.substring(0,path.lastIndexOf("\\"));

path += "\\images";

//圖片的url

String url = new String();

//創(chuàng)建文件夾

java.io.File file = new java.io.File(path);

file.mkdirs();

try{

// 上傳初始化

su.initialize(this.servlet.getServletConfig(),request,response);

//設(shè)定上傳限制

//1.限制每個(gè)上傳照片的最大長(zhǎng)度。

su.setMaxFileSize(5000000);

//2.限制總上傳數(shù)據(jù)的長(zhǎng)度。

su.setTotalMaxFileSize(10000000);

//3.設(shè)定允許上傳的照片(通過(guò)擴(kuò)展名限制)。

su.setAllowedFilesList("jpg,gif,GIF,JPG");

//上傳照片

su.upload();

//獲得請(qǐng)求的表單數(shù)據(jù)

String username = su.getRequest().getParameter("username");//username

String password = su.getRequest().getParameter("password");//password

String sex = su.getRequest().getParameter("sex");//sex

String email =su.getRequest().getParameter("email");//email

String apwd =su.getRequest().getParameter("apwd");//question of password

String rpwd =su.getRequest().getParameter("rpwd");//anser of password

userForm.setUsername(username);

userForm.setPassword(password);

userForm.setSex(sex);

userForm.setEmail(email);

userForm.setApwd(apwd);

userForm.setRpwd(rpwd);

//將上傳照片全部保存到指定目錄

if(!su.getFiles().getFile(0).isMissing()){

su.save(path,su.SAVE_PHYSICAL);

//文件名

filename = su.getFiles().getFile(0).getFileName();

//得到擴(kuò)展名

fileext = su.getFiles().getFile(0).getFileExt();

//給圖片改名,命名形如:20060814135411.gif

Utility u = new Utility();

String newName = u.getId()+"."+fileext;

java.io.File oldFile = new java.io.File(path+"\\"+filename);

java.io.File newFile = new java.io.File( path + "\\"+newName);

//上傳圖片的url

url = request.getContextPath()+"/images/"+newName;

if(oldFile!=null){

oldFile.renameTo(newFile);

}else{

result+="命名出錯(cuò)!";

}

System.out.println("0000000000000000000"+filename);

System.out.println("0000000000000000000"+path+"\\"+newName);

System.out.println("0000000000000000000"+url);

System.out.println(result);

}

}catch(com.jspsmart.upload.SmartUploadException e){

result += "file出錯(cuò)信息:";

result += e.getMessage();

System.out.println(result);

}

怎么用Java代碼實(shí)現(xiàn)上傳下載附件

int id = (Integer) request.getSession().getAttribute("id");

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);


本文名稱:java上傳附件功能代碼 java附件上傳功能實(shí)現(xiàn)
文章網(wǎng)址:http://weahome.cn/article/hiidhc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部