📄 tie3action.java
字号:
package struts.fatieaction;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;
import org.apache.struts.validator.DynaValidatorForm;
import domain.Offers;
public class Tie3Action extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
DynaValidatorForm dvf = (DynaValidatorForm)form;
String productname = dvf.getString("textfield");
String productprice = dvf.getString("textfield3");
String productnum = dvf.getString("textfield4");
int pn = Integer.parseInt(productnum);
double pp = Double.parseDouble(productprice);
String result = productname;
Offers of = new Offers();
if (pn <= 0) {
pn = 1;
}
// for (int i = 0; i < productname.length(); i++) {
// if (productname.charAt(i) == '/') {
// result = productname.substring(i + 1);
// break;
// }
// }
//开始上传文件
FormFile fileone = (FormFile)dvf.get("imageupload") ; //取得上传的文件名
System.out.println(fileone.getContentType());
if(fileone.getFileSize() > 0){
String s1 = fileone.getContentType();
String kuozhan = s1.substring(6);
String[] sty = {"jpg","gif","bmp","jpeg","JPG","GIF","BMP","JPEG","pjpeg"};
int count = 0;
for(int i = 0; i < sty.length ; i ++){
if(sty[i].equals(kuozhan)){
count = count + 1;
}
}
if(count == 0 || count > 1){
ActionMessages errros = new ActionMessages();
errros.add("errors.image", new ActionMessage("errors.image"));
this.saveErrors(request, errros);
return mapping.findForward("no");
}
if(fileone.getFileSize() > 3500000){
ActionMessages errros = new ActionMessages();
errros.add("errors.imageSize", new ActionMessage("errors.imageSize"));
this.saveErrors(request, errros);
return mapping.findForward("no");
}
String filePath = this.getServlet().getServletContext().getRealPath("/") ; //取得当前路径
InputStream stream = fileone.getInputStream() ; //把文件读入
String path=filePath+"images\\offerphotos\\";
/*
* 建立一个上传文件的输出流如果是linux系统请把"\\" 换成 "/"
*
* 现在文件上传的路径为-------->>>>>> path
*/
String iamgepath="images\\offerphotos\\"+fileone.getFileName();
of.setPhoto(iamgepath);
OutputStream bos = new FileOutputStream(path +fileone.getFileName());
request.setAttribute("fileName",filePath + "/" + fileone.getFileName());
int bytesRead = 0 ;
byte[] buffer = new byte[3500000] ;
while( (bytesRead = stream.read(buffer,0,3500000) ) != -1 )
{
bos.write(buffer,0,bytesRead) ;
}
bos.close();
stream.close() ;
//上传文件完成
}else{
of.setPhoto("没有图片");
}
System.out.println(result + ",你的出价:" + pp + ",要" + pn + "台");
request.getSession().setAttribute("name", result);
request.getSession().setAttribute("price", pp);
request.getSession().setAttribute("num", pn);
request.getSession().setAttribute("photop",of.getPhoto() );
return mapping.findForward("tie3");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -