java附件上传,如何以附件的形式上传

本文目录一览:

上传附件 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号

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:(根据不同情况要更改相关代码)

User userForm = new User();

String result = new String();

SmartUpload su = new SmartUpload();

//文件名

String filename = new String();

//文件扩展名

String fileext = new String();

//文件上传后存储的路径

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

path = path.substring(0,path.lastIndexOf(“\\”));

path += “\\images”;

//图片的url

String url = new String();

//创建文件夹

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

file.mkdirs();

try{

// 上传初始化

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

//设定上传限制

//1.限制每个上传照片的最大长度。

su.setMaxFileSize(5000000);

//2.限制总上传数据的长度。

su.setTotalMaxFileSize(10000000);

//3.设定允许上传的照片(通过扩展名限制)。

su.setAllowedFilesList(“jpg,gif,GIF,JPG”);

//上传照片

su.upload();

//获得请求的表单数据

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

//得到扩展名

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+=”命名出错!”;

}

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出错信息:”;

result += e.getMessage();

System.out.println(result);

}

java附件上传功能,上传的附件要根据时间来重命名,上传的路径保存在服务器指定目录根据年月来分的目录里

如果使用框架的话,比如 struts ,就比较简单了

获取上传的时间:

Calendar cal = Calendar.getInstance();

String year = String.valueOf(cal.get(Calendar.YEAR));

String month = String.valueOf(cal.get(Calendar.MONTH));

获取路径:

String path = ServletActionContext.getServletContext().getRealPath(“/”+year+”/”+month);

直接保存在 path 这个目录里面就可以

如果没有使用 框架,可以使用 FileUpload 这个 jar 包来上传文件

java 上传附件实现方法

第一,jsp上传页面内容:

%@ page contentType=”text/html; charset=GBK” %

%@ taglib uri=”/WEB-INF/struts-html.tld” prefix=”html” %

html

head

title

jsp1

/title

/head

body bgcolor=”#ffffff”

html:form action=”myupload.do” method=”post” enctype=”multipart/form-data”

html:file property=”thisFile”/br

html:file property=”thisFile”/br

html:submit/

/html:form

/body

/html

第二,一个javabean

package upload;

import org.apache.struts.action.ActionForm;

import org.apache.struts.upload.FormFile;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;

public class FileInfo extends ActionForm {

private FormFile thisFile;

public FormFile getThisFile() {

return thisFile;

}

public void setThisFile(FormFile thisFile) {

this.thisFile = thisFile;

}

public ActionErrors validate(ActionMapping actionMapping,

HttpServletRequest httpServletRequest) {

/** @todo: finish this method, this is just the skeleton.*/

return null;

}

public void reset(ActionMapping actionMapping,

HttpServletRequest servletRequest) {

}

}

第三,一个action

package upload;

import java.io.*;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.Action;

import org.apache.struts.upload.FormFile;

public class myupload extends Action {

public ActionForward execute(ActionMapping actionMapping,

ActionForm actionForm,

HttpServletRequest request,

HttpServletResponse response) throws

FileNotFoundException, IOException {

FileInfo fileInfo = (FileInfo) actionForm;

//获取上传文件

FormFile f=fileInfo.getThisFile();

InputStream is=f.getInputStream();

//将文件存入服务器上

String filename=request.getSession().getServletContext().getRealPath(“/shangchuan/”+f.getFileName());

OutputStream os=new FileOutputStream(filename);

int x=0;

//优化流处理过程

byte[] buffer = new byte[8192];

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

{

os.write(buffer,0,x);

}

os.close();

response.sendRedirect(“jsp1.jsp”);//根据实际情况跳转

return null;

}

}

原创文章,作者:RQYX,如若转载,请注明出处:https://www.506064.com/n/144277.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
RQYXRQYX
上一篇 2024-10-24 15:28
下一篇 2024-10-24 15:28

相关推荐

  • java client.getacsresponse 编译报错解决方法

    java client.getacsresponse 编译报错是Java编程过程中常见的错误,常见的原因是代码的语法错误、类库依赖问题和编译环境的配置问题。下面将从多个方面进行分析…

    编程 2025-04-29
  • Java JsonPath 效率优化指南

    本篇文章将深入探讨Java JsonPath的效率问题,并提供一些优化方案。 一、JsonPath 简介 JsonPath是一个可用于从JSON数据中获取信息的库。它提供了一种DS…

    编程 2025-04-29
  • Java Bean加载过程

    Java Bean加载过程涉及到类加载器、反射机制和Java虚拟机的执行过程。在本文中,将从这三个方面详细阐述Java Bean加载的过程。 一、类加载器 类加载器是Java虚拟机…

    编程 2025-04-29
  • Java腾讯云音视频对接

    本文旨在从多个方面详细阐述Java腾讯云音视频对接,提供完整的代码示例。 一、腾讯云音视频介绍 腾讯云音视频服务(Cloud Tencent Real-Time Communica…

    编程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介绍

    本文将详细介绍Java Milvus SearchParam withoutFields的相关知识和用法。 一、什么是Java Milvus SearchParam without…

    编程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java语言中的一个版本,于2014年3月18日发布。本文将从多个方面对Java 8中某一周的周一进行详细的阐述。 一、数组处理 Java 8新特性之一是Stream…

    编程 2025-04-29
  • Java判断字符串是否存在多个

    本文将从以下几个方面详细阐述如何使用Java判断一个字符串中是否存在多个指定字符: 一、字符串遍历 字符串是Java编程中非常重要的一种数据类型。要判断字符串中是否存在多个指定字符…

    编程 2025-04-29
  • VSCode为什么无法运行Java

    解答:VSCode无法运行Java是因为默认情况下,VSCode并没有集成Java运行环境,需要手动添加Java运行环境或安装相关插件才能实现Java代码的编写、调试和运行。 一、…

    编程 2025-04-29
  • Java任务下发回滚系统的设计与实现

    本文将介绍一个Java任务下发回滚系统的设计与实现。该系统可以用于执行复杂的任务,包括可回滚的任务,及时恢复任务失败前的状态。系统使用Java语言进行开发,可以支持多种类型的任务。…

    编程 2025-04-29
  • Java 8 Group By 会影响排序吗?

    是的,Java 8中的Group By会对排序产生影响。本文将从多个方面探讨Group By对排序的影响。 一、Group By的概述 Group By是SQL中的一种常见操作,它…

    编程 2025-04-29

发表回复

登录后才能评论