javaweb完整项目代码(如何看一个java web 项目代码)

本文目录
- 如何看一个java web 项目代码
- 做一个javaweb项目,会员登录后,点击谁来看过,会显示最近访问我的人的相关信息,求实现思路和主要代码
- 求java web 实现的文件上传代码 用开发环境是myeclipse
- javaweb中实现文件上传完整代码(servlet里面dopost的代码)
- javaweb项目代码中如何区别jsp和javascript(哪一部分是jsp代码,哪部分是javascript代码)
- java web项目代码包含哪些文件夹
如何看一个java web 项目代码
首先把项目跑起来,根据自己想看的功能跟下去。
***隐藏网址***
***隐藏网址***
如果是用spring mvc实现拦截的就找实现了spring标准的拦截位置
如果是struts等等根据该框架的标准找到后台控制层代码
我这里全部是讲的mvc模式。
如果是那种没使用mvc开发模式的,那你就可以不用看了,没有看的价值。因为过 了一个月他自己也看不懂自己的代码逻辑。
找到了控制层代码,那么一切就迎刃而解了 ,该打断点打断点,该跟代码的跟代码。。
后台数据库事物那种,就暂且不提了,懂的人一点就通,不懂的我解释也解释不清楚
做一个javaweb项目,会员登录后,点击谁来看过,会显示最近访问我的人的相关信息,求实现思路和主要代码
request.getParameter("用户名或者ID");
session.setAtrribute("key",value);
然后在别的页面可以通过session.getAttribute("key");得到用户名或者ID。不过这时得到的是一个Object类型的,你需要进行强制转换。。。
求java web 实现的文件上传代码 用开发环境是myeclipse
这个例子
***隐藏网址***
1、上传的前台页面:
《%@pagelanguage="java"import="java.util.*"contentType="text/html;charset=gbk" pageEncoding="gbk"%》 《html》 《body》
《form action="MultipartTestServlet" enctype="multipart/form-data" method="post"》
《input type="text" name="username" /》《br /》 《input type="file" name="myfile" /》《br/》 《input type="file" name="myfile" /》《br/》 《input type="submit" /》 《/form》 《/body》 《/html》
2、上传的后台代码:
后台代码是个servlet,很简单咯,觉得比ASP利用组件还简单。呵呵 package upload;
import java.io.File;
import java.io.IOException; import java.util.ArrayList
; import java.util.Iterator; import java.util.List;
import javax.servlet.ServletException;
***隐藏网址***
***隐藏网址***
***隐藏网址***
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;
public class MultipartTestServlet extends HttpServlet {
public MultipartTestServlet() { super(); }
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置request编码,主要是为了处理普通输入框中的中文问题 request.setCharacterEncoding("gbk");
//这里对request进行封装,RequestContext提供了对request多个访问方法 RequestContext requestContext = new ServletRequestContext(request); //判断表单是否是Multipart类型的。这里可以直接对request进行判断,不过已经以前的用法了
if(FileUpload.isMultipartContent(requestContext)){
DiskFileItemFactory factory = new DiskFileItemFactory(); //设置文件的缓存路径
factory.setRepository(new File("c:/tmp/"));
ServletFileUpload upload = new ServletFileUpload(factory); //设置上传文件大小的上限,-1表示无上限 upload.setSizeMax(100*1024*1024); List items = new ArrayList(); try {
//上传文件,并解析出所有的表单字段,包括普通字段和文件字段 items = upload.parseRequest(request); } catch (FileUploadException e1) {
System.out.println("文件上传发生错误" + e1.getMessage()); }
//下面对每个字段进行处理,分普通字段和文件字段 Iterator it = items.iterator(); while(it.hasNext()){
FileItem fileItem = (FileItem) it.next(); //如果是普通字段
if(fileItem.isFormField()){
System.out.println(fileItem.getFieldName() + " " + fileItem.getName() + " " + new String(fileItem.getString().getBytes("iso8859-1"), "gbk")); }else{
System.out.println(fileItem.getFieldName() + " " + fileItem.getName() + " " + fileItem.isInMemory() + " " + fileItem.getContentType() + " " + fileItem.getSize());
//保存文件,其实就是把缓存里的数据写到目标路径下 if(fileItem.getName()!=null && fileItem.getSize()!=0){ File fullFile = new File(fileItem.getName());
File newFile = new File("c:/temp/" + fullFile.getName()); try {
fileItem.write(newFile); } catch (Exception e) { e.printStackTrace(); } }else{
System.out.println("文件没有选择 或 文件内容为空"); } } } } } }
3、编译和配置servlet
先设置classpath,使它包含commons-fileUpload的jar包路径,然后进行编译即可,注意这里的包路径。
然后打开站点下的web-inf路径下的web.xml,在里面添加: 《servlet》
《servlet-name》MultipartTestServlet《/servlet-name》
《servlet-class》upload.MultipartTestServlet《/servlet-class》 《/servlet》 《servlet》
《servlet-name》dispatcher《/servlet-name》
《servlet-class》helloApp.DispatcherServlet《/servlet-class》 《/servlet》
好了,现在就可以文件上传了,不过运行之前记得在c:/下新建tmp和temp文件夹,别忘了,不然程序会报告错误的。
javaweb中实现文件上传完整代码(servlet里面dopost的代码)
structs
的
jsp
页面文件上传表单,只要项目是SSH的就行了
jsp:
《s:form
action="add.do"
id="inputForm"
enctype="multipart/form-data"
》
《td
》
《s:file
name="upload"
cssClass="{required:true}"
contenteditable="false"》《/s:file》
《span
class="field_tipinfo"》请选择文件《/span》
《/td》
《/s:form
action:
private
File
upload;//上传的文件
....
public
String
add()
throws
Exception
{
//保存文件
save(upload);
}
...
javaweb项目代码中如何区别jsp和javascript(哪一部分是jsp代码,哪部分是javascript代码)
通常javaweb页面里面主要包括java jsp和html这三类扩展名的文件(其他的配置文件等先不考虑)
java文件就是纯粹的java代码了,一般用于后台控制和业务逻辑处理等等
html文件是纯页面代码,但是也可以嵌套javascript,通常写在《script》《/script》内,或者直接在《head》《/head》中调用外部javascript代码
jsp文件是可以嵌套java代码的页面文件,可以包括通用html页面代码;也可以包括javacript代码,嵌套方式与html文件相同;还可以包含java代码(通常用于调用外部代码和一些简单的逻辑处理),包含在《% %》内
java web项目代码包含哪些文件夹
java zip 压缩文件以及整个文件夹目录
public static final String EXT = ".zip";
private static final String BASE_DIR = "";
// 符号"/"用来作为目录标识判断符
private static final String PATH = "/";
private static final int BUFFER = 1024;
/**
* 压缩
*
* @param srcPath 需要压缩的文件(目录) 的路径
* @throws Exception
*/
public static void compress(String srcPath) throws Exception {
File srcFile = new File(srcPath);
compress(srcFile);
}
/**
* 压缩
*
* @param srcFile 需要压缩的文件(目录) 的路径流
* @throws Exception
*/
public static void compress(File srcFile) throws Exception {
String name = srcFile.getName();
String basePath = srcFile.getParent();
String destPath = basePath +"/"+ name + EXT;
compress(srcFile, destPath);
}
/**
* 压缩
*
* @param srcFile
* 源路径
* @param destPath
* 目标路径
* @throws Exception
*/
public static void compress(File srcFile, File destFile) throws Exception {
// 对输出文件做CRC32校验
CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(
destFile), new CRC32());
ZipOutputStream zos = new ZipOutputStream(cos);
compress(srcFile, zos, BASE_DIR);
zos.flush();
zos.close();
}
/**
* 压缩文件
*
* @param srcFile
* @param destPath
* @throws Exception
*/
public static void compress(File srcFile, String destPath) throws Exception {
compress(srcFile, new File(destPath));
}
/**
* 压缩
*
* @param srcFile
* 源路径
* @param zos
* ZipOutputStream
* @param basePath
* 压缩包内相对路径
* @throws Exception
*/
private static void compress(File srcFile, ZipOutputStream zos,
String basePath) throws Exception {
if (srcFile.isDirectory()) {
compressDir(srcFile, zos, basePath);
} else {
compressFile(srcFile, zos, basePath);
}
}
/**
* 文件压缩
*
* @param srcPath
* 源文件路径
* @param destPath
* 目标文件路径
*
*/
public static void compress(String srcPath, String destPath)
throws Exception {
File srcFile = new File(srcPath);
compress(srcFile, destPath);
}
/**
* 压缩目录
*
* @param dir
* @param zos
* @param basePath
* @throws Exception
*/
private static void compressDir(File dir, ZipOutputStream zos,
String basePath) throws Exception {
File files = dir.listFiles();
// 构建空目录
if (files.length 《 1) {
ZipEntry entry = new ZipEntry(basePath + dir.getName() + PATH);
zos.putNextEntry(entry);
zos.closeEntry();
}
for (File file : files) {
// 递归压缩
compress(file, zos, basePath + dir.getName() + PATH);
}
}
/**
* 文件压缩
*
* @param file
* 待压缩文件
* @param zos
* ZipOutputStream
* @param dir
* 压缩文件中的当前路径
* @throws Exception
*/
private static void compressFile(File file, ZipOutputStream zos, String dir)
throws Exception {
/**
* 压缩包内文件名定义
*
* 《pre》
* 如果有多级目录,那么这里就需要给出包含目录的文件名
* 如果用WinRAR打开压缩包,中文名将显示为乱码
* 《/pre》
*/
ZipEntry entry = new ZipEntry(dir + file.getName());
zos.putNextEntry(entry);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
file));
int count;
byte data;
while ((count = bis.read(data, 0, BUFFER)) != -1) {
zos.write(data, 0, count);
}
bis.close();
zos.closeEntry();
}
/**
* 解压缩功能.
* 将ZIP_FILENAME文件解压到ZIP_DIR目录下.
* @throws Exception
*/
public static void upZipFile() throws Exception{
String ZIP_DIR="d:\\";
String ZIP_FILENAME="D:\\xxx.zip";
ZipFile zfile=new ZipFile(ZIP_FILENAME);
Enumeration zList=zfile.entries();
ZipEntry ze=null;
byte;
while(zList.hasMoreElements()){
ze=(ZipEntry)zList.nextElement();
if(ze.isDirectory()){
File f=new File(ZIP_DIR+ze.getName());
f.mkdir();
continue;
}
OutputStream os=new BufferedOutputStream(new FileOutputStream(getRealFileName(ZIP_DIR, ze.getName())));
InputStream is=new BufferedInputStream(zfile.getInputStream(ze));
int readLen=0;
while ((readLen=is.read(buf, 0, 1024))!=-1) {
os.write(buf, 0, readLen);
}
is.close();
os.close();
}
zfile.close();
}
/**
* 给定根目录,返回一个相对路径所对应的实际文件名.
* @param baseDir 指定根目录
* @param absFileName 相对路径名,来自于ZipEntry中的name
* @return java.io.File 实际的文件
*/
public static File getRealFileName(String baseDir, String absFileName){
String dirs=absFileName.split("/");
File ret=new File(baseDir);
if(dirs.length》1){
for (int i = 0; i 《 dirs.length-1;i++) {
ret=new File(ret, dirs);
}
if(!ret.exists())
ret.mkdirs();
ret=new File(ret, dirs);
return ret;
}
return ret;
}

更多文章:
jquery动态添加表格行(如何通过jquery动态给表格添加一行)
2026年10月23日 00:30
cmake命令行参数(CMake (三)cmake 在工程中的用法)
2026年10月23日 00:00
linux数据备份命令(Linux系统下常用的数据备份方法有哪些)
2026年10月22日 23:20
executequery返回值(ExecuteQuery(sql)的返回值)
2026年10月22日 23:00
舍曲林吃了一周越来越严重(我母亲77岁,服用舍曲林一周,开始半片,4天后改为1片,第三天开始失眠,第四天出现乏力、食欲下降,)
2026年10月22日 21:00





